Google Chrome的Javascript重定向问题
我的问题有点类似于这个问题。然而,由于我的实现有点不同,我提出了一个新问题 我有一个页面,其中后退按钮是被禁用的(在谷歌搜索了很多次后得到了脚本)。此页面的作用是重定向到其他位置(ASP.NET MVC控制器操作)。由于操作需要时间才能完成,因此会显示等待消息。下面是我用来禁用后退按钮和重定向页面的脚本Google Chrome的Javascript重定向问题,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我的问题有点类似于这个问题。然而,由于我的实现有点不同,我提出了一个新问题 我有一个页面,其中后退按钮是被禁用的(在谷歌搜索了很多次后得到了脚本)。此页面的作用是重定向到其他位置(ASP.NET MVC控制器操作)。由于操作需要时间才能完成,因此会显示等待消息。下面是我用来禁用后退按钮和重定向页面的脚本 <script type="text/javascript"> function changeHashOnLoad() { window.location.h
<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>
更新工作脚本
<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>
函数changehasonload(){
window.location.href+=“#”;
设置超时(再次更改,50);
}
函数changehash(){
window.location.href+=“1”;
}
var storedHash=window.location.hash;
window.setInterval(函数(){
if(window.location.hash!=storedHash){
window.location.hash=storedHash;
}
}, 50);
//在所有图像完成加载后执行此操作
$(窗口).load(函数(){
changehasonload();
//显示等待消息
$(“#domWaitMessage”).show();
//原始重定向代码
//window.location.href=document.forms[0]。操作;
//新的工作重定向代码
setTimeout(函数(){window.location.href=document.forms[0].action;},100);
});
不要使用getAttribute(“操作”)
尝试如下操作:
window.location.href = document.forms[0].action;
这在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>
函数changehasonload(){
window.location.href+=“#”;
设置超时(再次更改,50);
}
函数changehash(){
window.location.href+=“1”;
}
var storedHash=window.location.hash;
window.setInterval(函数(){
if(window.location.hash!=storedHash){
window.location.hash=storedHash;
}
}, 50);
//在所有图像完成加载后执行此操作
$(窗口).load(函数(){
changehasonload();
//显示等待消息
$(“#domWaitMessage”).show();
//重定向到新页面/控制器操作
window.location.href=document.forms[0]。操作;
});
你好
事实上,情况就是这样:
setTimeout(function () { window.location.href = document.forms[0].action; },100);
所以你可以说“为什么是铬?”?Chrome和他的浏览器比其他浏览器快得多。在50毫秒内,chrome仍然开始重定向,而FF和IE则没有 我怀疑这是原因,但仅供参考,您的
setTimeout
很奇怪,因为您在超时期间使用了字符串。此外,函数调用不需要字符串。您可以将该行替换为:setTimeout(changehash,50)代码>@Jacob,试过了,但还是不行。但是你的建议看起来干净多了,谢谢!。它对我有用。你能不能提供一个完整的页面作为例子,即带有action属性的表单?我也添加了HTML部分。
setTimeout(function () { window.location.href = document.forms[0].action; },100);