我的问题与this SO问题有些相似。但是,由于我的实现有点不同,我已经创建了一个新问题。
我有一个页面,其中禁用后退按钮(在大量谷歌搜索后获取脚本)。这个页面的作用是它重定向到不同的位置(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>
我的上述代码适用于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 Screenshot http://img43.imageshack.us/img43/638/200response.png
铬:
Chrome Screenshot 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>
更新工作脚本
<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>
答案 0 :(得分:5)
事实上,这就是:
函数changeHashOnLoad()设置超时,以便在历史记录中存储第二个哈希值('#1')。但是现在没有执行该功能(它将在50ms内)
主代码继续运行到重定向到document.forms [0]的行.action
仅限现在:执行timeedted函数changeHashAgain()。页面被重定向到'#1',重定向到document.forms [0] .action被取消
一种简单快捷的解决方法:延迟主重定向。
setTimeout(function () { window.location.href = document.forms[0].action; },100);
所以你可以说“为什么选择Chrome?” ? Chrome和他的javascript engine V8比其他浏览器快得多。在50ms,chrome仍然开始重定向,而FF和IE不是!
答案 1 :(得分:0)
而不是使用getAttribute("action")
尝试这样:
window.location.href = document.forms[0].action;
答案 2 :(得分:0)
这适用于我的chrome。由于您没有提供完整的代码,因此很难确定错误的位置,但它似乎不在上面的代码中。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="class.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>
<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>
</head>
<body>
<form method="post" action="gotothispage.page">
<div style="display: none" id="domWaitMessage">HELLO</div>
</form>
</body>
</html>