【发布时间】:2013-11-11 13:36:33
【问题描述】:
当我点击链接或刷新或关闭标签时,我有此代码提醒。
但我需要 only 在 close 窗口(选项卡)上发出警报。如何做到这一点?
我的网站上有许多外部和内部链接。
<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function (e) {
var e = e || window.event;
//IE & Firefox
if (e) {
e.returnValue = 'Are you sure?';
}
// For Safari
return 'Are you sure?';
};
</script>
</head>
<body>
<!-- this will ask for confirmation: -->
<!-- I need to alert for external links. -->
<a href="http://google.com">external link</a>
<!-- this will ask for confirmation: -->
<!-- I don't need to alert for local links in my web site -->
<a href="about.html">internal link</a>
</body>
</html>
【问题讨论】:
-
你可以在 link-onclick 调用一个监听器来重置你的 onbeforeunload 监听器。
-
我可以将 onclick 事件添加到将 onbeforeunload 设置为 null 的每个链接。但是我有很多链接。
-
哦,你的意思是只关闭,而不是点击链接时?所以我想你应该在你的例子中添加另一个“本地链接”与 cmets“这个不会问”
-
是的,仅对关闭和外部链接发出警报。
标签: javascript jquery window alert