如何使用 JavaScript 禁用浏览器的后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5947322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:38:10  来源:igfitidea点击:

How to disable back button of browser using JavaScript

javascripthtmlbrowser

提问by Krishna

I want to disable the back buttons in the browser using JavaScript. Can any one help me how to do that?

我想使用 JavaScript 禁用浏览器中的后退按钮。任何人都可以帮助我如何做到这一点?

回答by Aaron Digulla

Read here for several options to disable the back button.

阅读此处了解禁用后退按钮的几个选项

BUT: The question makes me feel like you're trying to solve the wrong problem.

但是:这个问题让我觉得你试图解决错误的问题。

The back button is something that the user expects to work. If you disable it, you would break the browser (from the user perspective): Something that works everywhere else doesn't work on your page. Users would hate your page and try to avoid it. If they can't avoid the page, they would hate it even more.

后退按钮是用户期望工作的东西。如果禁用它,则会破坏浏览器(从用户的角度来看):在其他任何地方都有效的东西在您的页面上无效。用户会讨厌您的页面并试图避免它。如果他们无法避开页面,他们会更讨厌它。

So what are your options:

那么你有哪些选择:

  1. You can get events when the user uses the button. GWT, for example, saves the internal state of the application in virtual history events. So the user can use the back button like Undo in a real application.

  2. Avoid creating history events for your page. Make sure the URL in the title bar never changes. Save all changes on your server. For the user, the page/application will feel like one page. When he returns next time, restore the last state from the database on your server.

    This way, the user can use the back button to leave your page as he is used to but he still won't loose any work.

  3. A mix of the two. For example stack overflow allows you to move between the pages using links and the back button. If you start to edit something on a page, you get a warning when you click on the back button.

  1. 您可以在用户使用按钮时获取事件。例如,GWT 将应用程序的内部状态保存在虚拟历史事件中。因此用户可以在实际应用程序中使用后退按钮,如撤消。

  2. 避免为您的页面创建历史事件。确保标题栏中的 URL 永远不会改变。保存服务器上的所有更改。对于用户来说,页面/应用程序感觉就像一页。当他下次回来时,从你服务器上的数据库中恢复上次的状态。

    这样,用户可以像习惯一样使用后退按钮离开您的页面,但他仍然不会丢失任何工作。

  3. 两者的混合。例如堆栈溢出允许您使用链接和后退按钮在页面之间移动。如果您开始编辑页面上的某些内容,则单击后退按钮时会收到警告。

回答by user746432

function noBack() {
    window.history.forward();
} 

回答by Ratul Ahmed

Simply open your page in a new tab using javascript like this

只需使用这样的 javascript 在新选项卡中打开您的页面

<script type="text/javascript" language="Javascript">window.open('yourpage.html');</script>

set this code to your previous page and this will open your page in a new tab and there will be no back button available.

将此代码设置为您的上一页,这将在新选项卡中打开您的页面,并且没有后退按钮可用。

回答by saidesh kilaru

 Simple logic: move forward when click back button
 ---------------------------------------------------- 

function preventBack() {
    window.history.forward();
}
 window.onunload = function() {
    null;
};
setTimeout("preventBack()", 0);


Keep this js code in your page it works.