Google Chrome的Javascript重定向问题

San*_*tak 2 javascript asp.net-mvc jquery

我的问题有点类似于这个问题.但是,由于我的实现有点不同,我创建了一个新问题.

我有一个页面,其中禁用后退按钮(经过大量谷歌搜索后得到脚本).这个页面的作用是它重定向到不同的位置(ASP.NET MVC控制器动作).由于操作需要一段时间才能完成,因此会显示等待消息.下面是我用来禁用后退按钮和重定向页面的脚本.

<script type="text/javascript">
    function changeHashOnLoad() {
        window.location.href += "#";
        setTimeout(changeHashAgain, 50);
    }

    function changeHashAgain() {
        window.location.href += "1";
    }

    var storedHash = window.location.hash;
    window.setInterval(function () {
        if (window.location.hash != storedHash) {
            window.location.hash = storedHash;
        }
    }, 50);

    //do after all images have finished loading
    $(window).load(function () {    
        changeHashOnLoad();        
        //show the wait message
        $("#domWaitMessage").show();
        //redirect to the new page/controller action
        window.location.href = document.forms[0].action;
    });
</script>
Run Code Online (Sandbox Code Playgroud)

我的上述代码适用于IE和Firefox,但不适用于Chrome.屏幕在Opera中闪烁很多,但重定向仍然有效.在Chrome中,我的控制器中的操作会被调用并且处理已完成,但在处理完成后页面未被重定向.大多数人可能觉得有必要改变流程/实施,但我对此事没有发言权,所以必须坚持这一流程.

Platform: ASP.NET MVC 2.0
jQuery Version: 1.4.1
Chrome Version: 16.0.912.63m

更新:

以下是从提琴手和Chrome网络标签中截取的屏幕截图.

Fiddler:
Fiddler截图http://img43.imageshack.us/img43/638/200response.png

Chrome:
Chrome屏幕截图http://img594.imageshack.us/img594/6381/chromenetwork.png

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Dummy Title</title>
</head>
<body>
    <form id="form1" method="post" action="Home/BookingConfirmation">    
        <div id="domWaitMessage">
            <img src="Content/images/preloader.gif" alt="Please Wait...." />
        </div>    
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

更新工作脚本

<script type="text/javascript">
    function changeHashOnLoad() {
        window.location.href += "#";
        setTimeout(changeHashAgain, 50);
    }

    function changeHashAgain() {
        window.location.href += "1";
    }

    var storedHash = window.location.hash;
    window.setInterval(function () {
        if (window.location.hash != storedHash) {
            window.location.hash = storedHash;
        }
    }, 50);

    //do after all images have finished loading
    $(window).load(function () {    
        changeHashOnLoad();        
        //show the wait message
        $("#domWaitMessage").show();
        //ORIGINAL REDIRECTION CODE
        //window.location.href = document.forms[0].action;
        //NEW WORKING REDIRECTION CODE
        setTimeout(function () { window.location.href = document.forms[0].action; }, 100);
    });
</script>
Run Code Online (Sandbox Code Playgroud)

Tho*_*ory 5

事实上,这是发生的事情:

  1. 函数changeHashOnLoad()设置超时,以便在历史记录中存储第二个哈希值('#1').但是现在没有执行该功能(它将在50ms内)

  2. 主代码继续运行到重定向到document.forms [0] .action的行

  3. 现在只有:执行了timeedted函数changeHashAgain().该页面被重定向到'#1',并且重定向到document.forms [0] .action被取消

一个简单快速的解决方法:延迟主重定向.

setTimeout(function () { window.location.href = document.forms[0].action; },100);
Run Code Online (Sandbox Code Playgroud)

所以你可以说"为什么选择Chrome?" ?Chrome和他的javascript引擎V8比其他浏览器快得多.在50ms,chrome仍然开始重定向,而FF和IE不是!