简体   繁体   中英

How do I disable back and forward button with javascript

I want to disable the back and forward action in the browser to my page. So how to disable the back and forward actions of the browser.

You can't. This is not something a browser exposes.

Regardless, this is a usability nightmare - I wouldn't recommend this, as having the back and forward buttons are things that users are very used to.

You can write your application in such a way that it "breaks" if back is used, but I would suggest that this would make your application mostly unusable to most people.

You can disable going back to a page using the following script in the head of your html.

<script type = "text/javascript" >
   function preventBack(){window.history.forward();}
    setTimeout("preventBack()", 0);
    window.onunload=function(){null};
</script>

whatch this SO question and answer and this SO Q&A

it's not exactly what you are looking for, but will give you an inspiration, how to react on user clicking back and forward buttons.

this jquery-plugin: jQuery history plugin is mentioned in both posts

Stumbled here from a google search. Hopefully this'll help those with the same issue.

After the mandatory mention of this usually being an usability problem, that should be solved elsewhere, I've found this to work in chrome 85:

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

Lastrly, in all fairness, credit goes to the following thread:

https://support.google.com/chrome/thread/8721521?msgid=10849294

You can disable the back button by

window.history.forward(1);

Hope this helps...:)

Cheers...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM