onbeforeunload 或单击浏览器后退按钮需要帮助
如果用户单击浏览器的后退按钮,那么我希望出现提示并要求确认。如果用户单击“确定”,则应导航至 xx.html
。如果用户单击“取消”,则应阻止导航。我该怎么做?
注意:我已经尝试过 onbeforeunload 方法,但它适用于所有导航操作。例如,单击页面上的链接也会触发此事件并向用户显示消息。
If a user clicks the browser's back button, then I want a prompt to appear and ask for confirmation. If the user clicks 'OK', then it should navigate to xx.html
. If the user clicks 'Cancel', then it should prevent the navigation. How can I do this?
Note: Already I tried the onbeforeunload
method, but it is working for all the navigation actions. For Example, clicking the link on the page will also fire this event and show the message to the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能。您能做的最好的事情就是使用
onbeforeunload
并在单击普通链接时删除该事件:You can't. The best you can do is use
onbeforeunload
and remove that event when clicking on a normal link:无法区分
onbeforeunload
事件中的导航操作。您可以通过删除每个链接的onclick
处理程序中的window.onbeforeunload
处理程序来禁用页面上的所有链接/表单,或者表单的 onsubmit
。但是,您仍然无法判断用户是否单击了后退、前进、书签等或在地址栏中输入了新地址。There's no way to differentiate between navigational actions in the
onbeforeunload
event. You could disable it for all links/forms on the page by removing thewindow.onbeforeunload
handler in each link'sonclick
handler, oronsubmit
for forms. However, you will still be unable to tell whether the user clicked back, forward, a bookmark, etc or typed a new address into the address bar.首先,您需要捕获“beforeunload”事件(这里之前的答案就是这样做的)。
更重要的是,您希望允许从某些元素进行不间断的导航(例如带有“allow-navigation”类的链接)。实现的方法是在单击您选择的链接时删除事件处理程序。
适用于您的链接类型:
这样您就可以控制页面导航。浏览器后退/前进/等仍然打开确认框。但您选择的链接将正常工作,不会出现任何中断。
希望这有帮助。
Firstly, you need to capture the "beforeunload" event (which previous answers here did).
More importantly, you want to allow non-disrupted navigation from certain elements (e.g. links with class "allow-navigation"). The way to achieve is to remove the event-handler when your links of choice are clicked.
Will work for your links of the type:
This way you can control the page navigation. Browser back/forward/etc still open the confirmation box. But the links you choose will work without any disruption.
Hope this helps.
编辑:
以上是所有卸载事件
http://jsfiddle.net/Lhw2x/85/
一直在通过其所有较低的对象搞乱它,它无法通过 javascript 完成,直到有人决定让所有主要浏览器为每个卸载事件使用特定名称,而不仅仅是一个卸载事件,
所以答案是否定的,你不能强制当他们尝试转到其他页面时,在确认上单击“确定”时导航到特定页面
,并且后退按钮没有特定的事件名称,它与卸载事件中的多个不同事件共享其事件,因此您无法返回通过javascript实现按钮特定功能
Edit:
above is for all unload events
http://jsfiddle.net/Lhw2x/85/
been messing with that through all its lower objects it cant be done through javascript till someone decides to make all major browsers use a specific name for each unload event instead of it just being an unload event
so the answer is no you cannot force a navigation to a specific page when they click okay on the confirmation when they try to go to a different page
and back button does not have a specific event name it shares its event with several different events all in the unload event so you cannot make a back button specific function through javascript