简体   繁体   English

如何使用 javascript 禁用后退和前进按钮

[英]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.可以编写应用程序,使其在使用 back 时“中断”,但我建议这会使您的应用程序对大多数人来说几乎无法使用。

You can disable going back to a page using the following script in the head of your html.您可以使用 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 whatch 这个 SO question and answerthis 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这个 jquery-plugin: jQuery 历史插件在两个帖子中都有提到

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:在强制提及这通常是一个可用性问题之后,应该在其他地方解决,我发现它可以在 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 https://support.google.com/chrome/thread/8721521?msgid=10849294

You can disable the back button by您可以通过以下方式禁用后退按钮

window.history.forward(1);

Hope this helps...:)希望这可以帮助...:)

Cheers...干杯...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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