我尝试使用以下代码在关闭浏览器窗口时收到警报:
window.onbeforeunload = confirmExit; function confirmExit() { return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; }
它可以工作,但是如果页面包含一个超链接,则单击该超链接会引发相同的警报。仅在关闭浏览器窗口时才需要显示警报,而在单击超链接时则不需要。
保持您的代码不变,并使用jQuery处理链接:
$(function () { $("a").click(function { window.onbeforeunload = null; }); });