Javascript IE 与 window.location.href 不兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10201809/
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
IE incompatability with window.location.href
提问by djq
I'm using a callback from an AJAX post request to navigate to a new page, but it is not working on Internet Explorer. My code is as follows:
我正在使用来自 AJAX 发布请求的回调来导航到新页面,但它在 Internet Explorer 上不起作用。我的代码如下:
$.ajax({
type: "POST",
url: phpUrl,
data: data,
async: false,
success: function() {
if (navigator.appName == 'Microsoft Internet Explorer'){ window.location.href("/step2.php")}
else{ window.location.href = "/step2.php"}
},
dataType:'json'
});
This works fine on FF/Safari/Chrome but when I test it on IE it does not work. Is there a better way of redirecting to a new page? I'm using async:false
as my data was not loading on Chrome/Safari if I did not use a callback as the page would just change before the POST
request was complete.
这在 FF/Safari/Chrome 上运行良好,但是当我在 IE 上测试时它不起作用。有没有更好的重定向到新页面的方法?我正在使用,async:false
因为如果我不使用回调,我的数据不会加载到 Chrome/Safari 上,因为页面会在POST
请求完成之前发生变化。
回答by josh3736
It's the parentheses. href
is not a function, so trying to invoke it—window.location.href("/step2.php")
—is a TypeError
.
是括号。 href
不是一个函数,所以试图调用它window.location.href("/step2.php")
——是一个TypeError
.
Assign to href
like you do on the next line, or better, use location.assign()
:
href
像您在下一行所做的那样分配,或者更好地使用location.assign()
:
location.assign('/step2.php');
While you can directly assign to location
's properties (location.href='...';
) to cause the browser to navigate, I recommend against this.
虽然您可以直接分配 tolocation
的属性 ( location.href='...';
) 来使浏览器导航,但我建议您不要这样做。
Internally, doing so is just calling location.assign()
anyway, and assigning to properties does not always behave the same in all browsers.
在内部,这样做只是调用location.assign()
,并且分配给属性在所有浏览器中的行为并不总是相同。
Regarding, async:false
, neverdo that. If you make a synchronous XHR request, you're doing it wrong.8.4%of reported IE9 hangs were due to synchronous XHR blocking the browser.
至于,async:false
,从来没有做到这一点。如果您发出同步 XHR 请求,那么您就做错了。8.4%的报告 IE9 挂起是由于同步 XHR 阻塞了浏览器。
Given that you have it in a callback, the assignment to location
won't happen until the POST completes, so I'm not sure what you mean by "the page would change before the POST completes." (Did you forget to cancel a form's submit
?)
鉴于您在回调中拥有它,location
在 POST 完成之前不会发生分配,所以我不确定您所说的“页面将在 POST 完成之前更改”是什么意思。(您是否忘记取消表单的submit
?)
回答by Chris Li
window.location.href = "/step2.php"
is just fine.
window.location.href = "/step2.php"
很好。
回答by user3012702
IE only like full url.
IE 只喜欢完整的 url。
var fullURL = 'http://www.your_site.com/step2.php';
$.ajax({
type: "POST",
url: phpUrl,
data: data,
async: false,
success: function() {
window.location.href(fullURL);
},
dataType:'json'
});