我尝试了以下代码,以便在关闭浏览器窗口时获取一个警告:
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?";
}
它能够工作,但如果页面包含一个超链接,点击该超链接会引发相同的警报。我需要仅在关闭浏览器窗口时显示警报,而不是在点击超链接时。