Google Chrome的Javascript重定向问题

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

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

 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; });  

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

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

更新:

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

小提琴手:
Fiddler截图http://img43.imageshack.us/img43/638/200response.png

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

HTML

   Dummy Title   
Please Wait....

更新工作脚本

  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); });  

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

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

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

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

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

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

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

而不是使用getAttribute("action")尝试这样:

 window.location.href = document.forms[0].action; 

这适用于我的铬。 由于您没有提供完整的代码,因此很难确定错误的位置,但它似乎不在上面的代码中。