dpgkg42484 2014-12-05 05:46
浏览 43
已采纳

防止浏览器和警告,严格的方式需要

I need to prevent the user to switch back to previous pages strictly. I show you example coding as follows:

 <html>
 <head>
 <script type="text/javascript">   

/* prevent back  */
window.history.forward();
    function prevent()
    {
window.history.forward(); 
    }


/* warning on unload */
window.onbeforeunload = function (e) { var message = 'Are you sure ?'; if (typeof e == 'undefined') { e = window.event; } if (e) { e.returnValue = message; } return message; }


 </script>
 </head>
    <body onload="prevent();" onpageshow="if (event.persisted) prevent();" onunload="">
        <!--content here-->
    <a href="in.html">in</a>
    </body>
 </html>

The above page have link to another page, if i click back on that second page, it come back to this first page and showing warning and then move to second page;

Second page:

<a href="hello.html">hello</a>

I added warning script for some reason.

Now, what i need exactly is. When prevent back working, warning script should not work.

1.When user unload a page, should show warning script 2.when prevent back function unload the page, should not show warning script

Is there any way possible to disable going back from a page in all the way, any strict way? the above coding is not much strict. it will move forward on getting back.

Thanks.

  • 写回答

1条回答 默认 最新

  • dream_wu2015 2014-12-05 06:47
    关注

    Yeah, boi.

    But seriously, this baby don't got back

    page 1

    <html>
        <head>
             <title>if you go, don't come back</title>
         </head>
         <body>
                <!--content here-->
             <a href="pageofnoreturn.html#noBack">in</a>
        </body>
    </html>
    

    pageofnoreturn.html

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误