javascript onbeforeunload 事件在 IE9 中过于热情
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7263309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onbeforeunload event is too enthusiastic in IE9
提问by teedyay
Here's some sample test html:
这是一些示例测试 html:
<!DOCTYPE html>
<html>
<body>
<a href="javascript:alert('Not going anywhere!');">Go nowhere 1</a>
<a href="javascript:void(0);" onclick="alert('Not going anywhere!');">Go nowhere 2</a>
<a href="http://www.google.com">Go somewhere</a>
<script type="text/javascript">
window.onbeforeunload = function() { return "Really leave?"; };
</script>
</body>
</html>
This is available as a working page here: timjeanes.com/ie9_onbeforeunload.htm
可在此处作为工作页面使用:timjeanes.com/ie9_onbeforeunload.htm
In Firefox, Chrome and Safari, I get the behaviour I want. That is, clicking either of the first two links shows an alert dialog; clicking the third link (or otherwise navigating away) shows the "Are you sure you want to leave this page?" message.
在 Firefox、Chrome 和 Safari 中,我得到了我想要的行为。也就是说,单击前两个链接中的任何一个都会显示警告对话框;单击第三个链接(或以其他方式离开)显示“您确定要离开此页面吗?” 信息。
However, in IE9, the "Are you sure you want to leave this page?" message also shows when you click either of the first two links, even though no navigation is taking place - we're just running some javascript.
但是,在 IE9 中,“您确定要离开此页面吗?” 当您单击前两个链接中的任何一个时,消息也会显示,即使没有进行导航 - 我们只是在运行一些 javascript。
Is there something I'm doing wrong? Or is there a nice workaround for IE?
有什么我做错了吗?或者 IE 有一个很好的解决方法吗?
One option would be to use href="#" and put my javascript in the onclick. I'm not keen on that as it takes the user to the top of the page, and my real-life page is quite tall.
一种选择是使用 href="#" 并将我的 javascript 放在 onclick 中。我并不热衷于它,因为它会将用户带到页面的顶部,而且我的真实页面非常高。
I've not tested in other versions of IE.
我没有在其他版本的 IE 中测试过。
采纳答案by gilly3
That's a pretty unfortunate bug. It seems you'll have to work around it, and using the hash link method is probably the best way. To avoid taking the user to the top of the page, cancel the event using event.preventDefault()
and event.returnValue = false
.
这是一个非常不幸的错误。看来您必须解决它,使用散列链接方法可能是最好的方法。为避免将用户带到页面顶部,请使用event.preventDefault()
和取消事件event.returnValue = false
。
function myClickHandler(e) {
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
alert("Not going anywhere!");
}
Call it like this:
像这样调用它:
<a href="#" onclick="myClickHandler(event)">Go nowhere 1</a>
Edit:You could cancel the event for all the hash-links on your page like this:
编辑:您可以取消页面上所有哈希链接的事件,如下所示:
var links = document.links;
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link.href == "#") {
if (link.addEventListener) {
link.addEventListener("click", cancelEvent, false);
}
else if (link.attachEvent) {
link.attachEvent("onclick", cancelEvent);
}
}
}
function cancelEvent(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
Or with jQuery using just one line of code:
或者使用 jQuery 只使用一行代码:
$("a[href='#']").click(function (e) { e.preventDefault(); });
回答by CpnCrunch
A simpler solution is to use onunload in IE and onbeforeunload in other browsers.
更简单的解决方案是在 IE 中使用 onunload,在其他浏览器中使用 onbeforeunload。
回答by Stuart Green
#null
in href
to prevent page jumping rather than just #
and script in onclick=""
#null
inhref
防止页面跳转而不仅仅是#
脚本onclick=""
回答by John Hartsock
You should not bind an event for click using href="javascript:...
: Below is not good.
您不应该使用href="javascript:...
以下方法绑定单击事件:下面不好。
<a href="javascript:alert('Not going anywhere!');">Go nowhere 1</a>
If your going to use an onclick and void the href then just return false in the onclick.
如果您要使用 onclick 并使 href 无效,则只需在 onclick 中返回 false。
<a onclick="alert('Not going anywhere!');return false;">Go nowhere 2</a>
回答by clarkb86
It's actually not a bug that the "Are you sure you want to leave this page?" message pops up when you click the first links; it seems to be a "feature" in IE 9 - More reasons I dislike IE
“您确定要离开此页面吗?”实际上不是错误。单击第一个链接时会弹出消息;它似乎是 IE 9 中的一个“功能”——我不喜欢 IE 的更多原因