javascript document.location 不会改变 IE9 中的网页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8245547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
document.location does not change the webpage in IE9?
提问by dgivoni
I am trying to redirect to a different page in IE9 (9.0.3).
我正在尝试重定向到 IE9 (9.0.3) 中的其他页面。
When I try to get/set document.location
, or document.location.href
, or window.location
/window.location.href
, I'm unable to do so. It fails without giving any errors.
当我尝试 get/set document.location
, or document.location.href
, or window.location
/ 时window.location.href
,我无法这样做。它失败而没有给出任何错误。
I've tried to check whether the document and windows objects are set, and they are, so I have no idea why the location object is "missing".
我试图检查是否设置了文档和窗口对象,它们是,所以我不知道为什么位置对象“丢失”。
I tried getting the document.URL
and that works fine, but it's read-only.
我尝试获取document.URL
并且效果很好,但它是只读的。
Anyone know what the problem is or how to achieve this in a cross-browser way?
任何人都知道问题是什么或如何以跨浏览器的方式实现这一目标?
回答by denmark dinga
I was also experiencing the same problem but found that adding
我也遇到了同样的问题,但发现添加
window.event.returnValue = false;
above line in the javascript before the redirection resolved the problem.
重定向之前javascript中的上述行解决了问题。
回答by Viruzzo
看到这个:http: //social.msdn.microsoft.com/Forums/en/iewebdevelopment/thread/c864ae63-66f6-4656-bcae-86b0018d70c9
Apparently it's a caching bug, you can solve it by appending a timestamp to the destination URL (that is, using a "unique" URL every time).
显然这是一个缓存错误,您可以通过向目标 URL 附加时间戳来解决它(即,每次都使用“唯一”URL)。
回答by Joshua
Perhaps your IE9 has some security restrictions in place that prevent JavaScript from directing URL's. window.location.href = "" should work normally on IE9.
也许您的 IE9 有一些安全限制,阻止 JavaScript 定向 URL。window.location.href = "" 应该可以在 IE9 上正常工作。
回答by Sudhir Bastakoti
Cache may be the reason, try:
缓存可能是原因,请尝试:
location.href='something.php?tmp=' + Date.parse(new Date())
Hope it helps
希望能帮助到你
回答by Gregory Bolkenstijn
You should use an absolute URL:
您应该使用绝对 URL:
var url = '/section/page/';
var host = window.location.hostname;
window.location = 'http://' + host + url;
var url = '/section/page/';
var host = window.location.hostname;
window.location = 'http://' + 主机 + url;
Where url is the relative path to your page.
其中 url 是您页面的相对路径。