防止刷新后返回不工作

prevent back not working after refresh

本文关键字:工作 返回 刷新      更新时间:2023-09-26

我从这里阻止了回编码

防止返回浏览器和警告,需要严格的方法

用户刷新后它不起作用,托德,帮助我解决此错误。

参考代码如下:

<html>
    <head>
         <title>Stay put, or else</title>
     </head>
     <body>
         <h1>welcome to purgatory</h1>
        <script>
            if (location.hash == '#noBack') {
                history.pushState(null, '', '#sit');
                //whatever hashes that suit your project
                window.onhashchange = function() {
                    if (location.hash == '#noBack') {
                        history.pushState(null, '', '#sit');
                    }
                }
            }
        </script>
    </body>
</html>

[注意:这是OP提供的代码的变通方法。

根据您的代码,当您导航到炼狱[第二]页面时,您正在连接一个主题标签"#noBack"。因此,在炼狱页面上,如果找到位置具有主题标签"#noBack"并将主题标签更改为"#sit"的条件,浏览器会解释脚本。

当您这次刷新页面时,它具有主题标签"#sit",我使用另一个条件进行处理,只需将主题标签更改回"#noBack"。因此,您将在if和else-if之间切换,并且不会转到上一页。

<script>        
    if (location.hash == '#noBack') {
        history.pushState(null, '', '#sit');
        //whatever hashes that suit your project
        window.onhashchange = function() {
            if (location.hash == '#noBack') {
                history.pushState(null, '', '#sit');
            }
        }
    } else if(location.hash == "#sit"){
        history.pushState(null, '', '#noBack');
        window.onhashchange = function() {
            if (location.hash == '#sit') {
                history.pushState(null, '', '#noBack');
            }
        }
    }
</script>