防止返回浏览器和警告,需要严格的方法 [英] prevent back in browser and warning, strict way needed

查看:76
本文介绍了防止返回浏览器和警告,需要严格的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要防止用户严格切换回上一页.我为您展示示例编码,如下所示:

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;

第二页:

<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.当用户卸载页面时,应显示警告脚本 2.当防止后退功能卸载页面时,不应显示警告脚本

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.

谢谢.

推荐答案

是的,鲍伊.

但是,很遗憾,这个婴儿没有回来

第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

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>

这篇关于防止返回浏览器和警告,需要严格的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆