jQuery 如何禁用某些页面上的后退按钮导航
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3359941/
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
How to disable back button navigation on certain pages
提问by user339108
Possible Duplicates:
How do I stop the Back and Refresh buttons from resubmitting my form?
Disable browser’s back button
Env: jQuery,richfaces
环境: jQuery,richfaces
How to disable back button navigation (in the menu bar) on certain pages of the user interface.
如何在用户界面的某些页面上禁用后退按钮导航(在菜单栏中)。
回答by thomasrutter
You cannot disable the back button on a user's browser. It's a fundamental feature of browsers which can't be overridden.
您无法禁用用户浏览器上的后退按钮。这是浏览器的基本功能,无法覆盖。
You canmake it so that your application breaks (displays an error message, requiring the user to start over) if the user goes back. It's a bad idea to do this, because it is really an admission that you didn't take the back button into account when you designed the application. Every application, even order forms, shopping carts, online banking etc, if designed correctly should be able to use the back button. If they can't, it's a failure on the part of the application developer.
如果用户返回,您可以使您的应用程序中断(显示错误消息,要求用户重新开始)。这样做是个坏主意,因为这实际上是在承认您在设计应用程序时没有考虑后退按钮。每个应用程序,甚至订单、购物车、网上银行等,如果设计正确,都应该能够使用后退按钮。如果他们不能,那就是应用程序开发人员的失败。
One approach I have seen for deliberately breaking the back button use is to pass a token on every URL within the application, and within every form. The token is regenerated on every page, and once the user loads a new page any tokens from previous pages are invalidated. When the user loads a page, the page will only show if the correct token (which was given to all links/forms on the previous page) was passed to it.
我见过的一种故意破坏后退按钮使用的方法是在应用程序中的每个 URL 和每个表单中传递一个令牌。令牌会在每个页面上重新生成,一旦用户加载新页面,之前页面中的所有令牌都会失效。当用户加载页面时,页面只会显示是否传递了正确的令牌(已提供给上一页上的所有链接/表单)。
The online banking application my bank provides is like this. If you use the back button at all, no more links will work and no more page reloads can be made - instead you see a notice telling you that you cannot go back, and you have to start over.
我的银行提供的网上银行应用程序是这样的。如果您完全使用后退按钮,则不再有链接可用,也无法重新加载页面——相反,您会看到一条通知,告诉您无法返回,必须重新开始。
Now, I see this as a lazy way of dealing with a problem. Online banking applications that do this would do it because it's theoretically easier to secure the application if they severely limit the number of circumstances under which someone may arrive at a given page. It is definitely a short-cut compared to making the application work properlyin the way that the usershould expect, but those who design online banking applications are less focused on usability and more focused on reducing potential attack vectors. They're also probably also stressed out from having to deal with really ancient computer systems and interfaces.
现在,我认为这是处理问题的一种懒惰方式。执行此操作的在线银行应用程序会这样做,因为如果它们严格限制某人可能到达给定页面的情况数量,则理论上更容易保护应用程序。与使应用程序以用户期望的方式正常工作相比,这绝对是一条捷径,但设计在线银行应用程序的人不太注重可用性,而更注重减少潜在的攻击媒介。他们也可能因为不得不处理非常古老的计算机系统和接口而感到压力。
回答by jAndy
You are not allowed to modify
the window.history
entrys. If you try to set window.history.length
most browsers will throw an exception.
你不允许modify
的window.history
entrys。如果尝试设置window.history.length
大多数浏览器都会抛出异常。
But you can do a more smooth thing with onbeforeunload
like
但是你可以用onbeforeunload
like做更流畅的事情
window.onbeforeunload = function(){
return "are you sure?";
};
that will fire a confirmation box when the user trys to leave the current page (including, history back/forward).
当用户尝试离开当前页面(包括历史后退/前进)时,将触发一个确认框。