防止使用JavaScript导航网页 [英] Prevent a webpage from navigating away using JavaScript
本文介绍了防止使用JavaScript导航网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何防止网页使用JavaScript导航?
How to prevent a webpage from navigating away using JavaScript?
推荐答案
使用 onunload
只允许您显示消息,但不会中断导航(因为为时已晚)。但是,您可以使用 onbeforeunload
并且它将中断导航:
Using onunload
only allows you to display messages, but it will not interrupt the navigation (because it is too late). However, you can use onbeforeunload
and it will interrupt navigation:
window.onbeforeunload = function() {
return "Are you sure you want to navigate away?";
}
修改:已移除在return语句中确认()
因为这导致了预期的确认窗口,但也显示第二次确认与第一次确认的结果。
Edit: Removed confirm()
in return statement as this caused a confirm window as expected, but also showed a second confirm with the result of the first confirm.
这篇关于防止使用JavaScript导航网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文