Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - I want to disable browsers back button, i have tried a code which is working for internet explorer but facing problem in chrome

It is working good in internet exlporer but in chrome it works only if the user clicks on somewhere in the page i.e., works only when the user gives a click on that page at least once before pressing browsers back button. I want to make it work as same as in internet explorer. here is my code:

    <html>
<head>
<script>history.pushState(null,null,location.href);
    window.onpopstate=function()
    {
        history.go(1);
        }

</script>
<body>
<a location.href="home.php" rel="index,follow"></a>
</body> 
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've stumbled on this question quite a lot and I had to achive the same thing

might not be the best solution, but I've made it to work in browsers IE, Explorer, Chrome, did not test Firefox tho, with this code:

   <script type = "text/javascript" >
        history.pushState(null, null, location.href);
        history.back();
        history.forward();
        window.onpopstate = function () {
            history.go(1);
        };
    </script>

The problem with Chrome is that it doesn't trigger onpopstate event unless you make browser action ( i.e. call history.back). Thats why I've added those to script. After adding only that made it work in Chrome but other browsers stopped adding history.forward fixed it and started to work on every browser mentioned.

Source if you want to know more or at least what helped me solve this

Hope it helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...