Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google Chrome的Javascript重定向问题_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

Google Chrome的Javascript重定向问题

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

我的问题有点类似于这个问题。然而,由于我的实现有点不同,我提出了一个新问题

我有一个页面,其中后退按钮是被禁用的(在谷歌搜索了很多次后得到了脚本)。此页面的作用是重定向到其他位置(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>
更新工作脚本

<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]。操作;
});
你好

事实上,情况就是这样:

  • 函数changeHashOnLoad()设置一个超时,以便在历史记录中存储第二个哈希值(“1”)。但该功能现在未执行(将在50毫秒后执行)

  • 主代码继续运行到重定向到document.forms[0]的行。操作

  • 现在仅执行:执行timeouted函数changehashreach()。页面重定向到“#1”,重定向到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);