如何防止关闭浏览器窗口? [英] How to prevent closing browser window?

查看:138
本文介绍了如何防止关闭浏览器窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码在关闭浏览器窗口时收到提醒:

I tried the following code to get an alert upon closing a browser window:

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?";
}

它有效,但如果页面包含一个超链接,则单击该超链接会引发同样的警报。我只有在关闭浏览器窗口而不是单击超链接时才需要显示警报。

It works, but if the page contains one hyperlink, clicking on that hyperlink raises the same alert. I need to show the alert only when I close the browser window and not upon clicking hyperlinks.

推荐答案

保持代码不变使用jQuery来处理链接:

Keep your code as is and use jQuery to handle links:

$(function () {
  $("a").click(function {
    window.onbeforeunload = null;
  });
});

这篇关于如何防止关闭浏览器窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆