【问题标题】:How can I make window.location.replace work in IE 9?如何使 window.location.replace 在 IE 9 中工作?
【发布时间】:2012-06-09 10:16:26
【问题描述】:

我有以下代码检查用户名和密码,如果成功则重定向到新页面。它在 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.");   
    }
},

【问题讨论】:

    标签: php javascript jquery internet-explorer


    【解决方案1】:

    我一直在寻找如何在 IE 上更改 window.location,这对我有用:

    var targetlocation = "mylocation";
    
    setTimeout(changeURL,0)
    
    function changeURL()
    {
        window.location = targetlocation;
    }
    

    【讨论】:

    • +1 正确,请参阅this post 了解其工作原理。
    【解决方案2】:
    if(html) {
      window.location.href = "filename.php";
    } else {
      $("#add_err").html("Wrong username or password.");
    }
    

    【讨论】:

    • location.href 是一个属性,而不是一个函数。这将失败。
    • -1 在您更改后撤回,现在是正确的 :) 但是没有 +1,因为更改 location.href 与 location.replace 不同,并且无需从中删除 location.replace OP的答案:(
    【解决方案3】:

    首先关于其他答案:location.replace 与仅更改 location.href 不同,它对历史的反应不同,在许多情况下 location.replace 对用户体验更好,所以不要把毛巾扔到现场。换得真快!

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

    然后我在我的 IE9 上测试了 console.log 并且它卡住了(这显然只有在我正在阅读的某个开发人员面板或选项卡或其他东西打开时才有效,尽管我对 IE9 的开发工具没有太多经验可以说那是肯定的)。

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

    【讨论】:

    • 很有趣...它是 console.log... 将提出一个关于 .location.href 和 .location.replace 之间区别的新问题...
    【解决方案4】:
    window.location = "newpage.php";
    

    window.location.href = "newpage.php";
    

    【讨论】:

      【解决方案5】:

      试试

      window.location.href = 'newpage.php'

      【讨论】:

        【解决方案6】:

        您不需要更换。

        if(html=='true') {
                window.location = "newpage.php";
               // or
               window.location.href = "newpage.php";
            } else {
                $("#add_err").html("Wrong username or password.");   
            }
        

        【讨论】:

        • 我不同意。 location.href 和 location.replace 的最大区别在于浏览器历史记录。 location.replace 会将页面从用户历史记录中取出,location.href 不会。有关更多信息,请参阅linklink
        猜你喜欢
        • 2013-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-20
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        • 2015-07-22
        相关资源
        最近更新 更多