如何在IE 9中使window.location.replace工作?

Nin*_*Cat 3 javascript php jquery internet-explorer

我有以下代码检查用户名和密码,然后重定向到新页面,如果它成功.它适用于Firefox就好了,但是当我在IE 9上试用它时,它只是坐着并挂起.我需要做什么才能在IE上工作?

username=$("#user_name").val();
password=$("#password").val();
$.ajax({
    type: "POST",
    url: "login.php",
    data: "name="+username+"&pwd="+password,
    success: function(html){
    console.log(html);
    if(html=='true') {
        window.location.replace("newpage.php");
    } else {
        $("#add_err").html("Wrong username or password.");   
    }
},
Run Code Online (Sandbox Code Playgroud)

Jim*_*nny 12

首先,关于其他答案:location.replace与更改location.href不同,它对历史的反应不同,在很多情况下,location.replace对用户体验更好,所以不要扔掉毛巾.在location.replace非常快!

所以我刚刚在我的机器上测试了IE9中的location.replace并且它工作正常.

然后我在我的IE9上测试了console.log并且它被阻塞(显然只有在某个开发人员面板或标签或某些东西打开时我才能阅读,但我对IE9的开发工具没有太多经验可以说肯定).

我打赌你的问题不是location.replace函数,但实际上是它之前的console.log函数!


小智 5

好吧,我一直在寻找如何更改IE上的window.location的方法,这对我来说很有效:

var targetlocation = "mylocation";

setTimeout(changeURL,0)

function changeURL()
{
    window.location = targetlocation;
}
Run Code Online (Sandbox Code Playgroud)