onbeforeunload上需要帮助或单击浏览器后退按钮 [英] Help required on onbeforeunload or click on browser back button
问题描述
如果用户点击浏览器的后退按钮,那么我想要出现提示并要求确认。如果用户点击确定,则应该导航到 xx.html
。如果用户点击取消,则应该阻止导航。我该怎么做?
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?
注意:我已经尝试了 onbeforeunload
方法,但它适用于所有导航动作。例如,单击页面上的链接也会触发此事件并向用户显示消息。
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.
推荐答案
你可以'吨。您可以做的最好是使用 onbeforeunload
并在点击普通链接时删除该事件:
You can't. The best you can do is use onbeforeunload
and remove that event when clicking on a normal link:
<script type="text/javascript">
window.onbeforeunload = function() {return "Are you sure?"}
</script>
<a href="somewhere.html" onclick="window.onbeforeunload = null;">Leave</a>
这篇关于onbeforeunload上需要帮助或单击浏览器后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!