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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:17:26  来源:igfitidea点击:

IE incompatability with window.location.href

javascriptjqueryajaxinternet-explorer

提问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:falseas my data was not loading on Chrome/Safari if I did not use a callback as the page would just change before the POSTrequest was complete.

这在 FF/Safari/Chrome 上运行良好,但是当我在 IE 上测试时它不起作用。有没有更好的重定向到新页面的方法?我正在使用,async:false因为如果我不使用回调,我的数据不会加载到 Chrome/Safari 上,因为页面会在POST请求完成之前发生变化。

回答by josh3736

It's the parentheses. hrefis 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 hreflike 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 locationwon'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'         
});