admin管理员组文章数量:1356808
The problem is as follows.
onbeforeunload
works like a charm in Firefox and has e.explicitOriginalTarget.activeElement
that shows what element has been clicked to cause it.
window.onbeforeunload = function(e){
if (e.explicitOriginalTarget.activeElement){
return;
}
In Chrome the 'e
' object looks identical when you close the window or click the link.
Is there any way to determine the target in chrome?
The problem is as follows.
onbeforeunload
works like a charm in Firefox and has e.explicitOriginalTarget.activeElement
that shows what element has been clicked to cause it.
window.onbeforeunload = function(e){
if (e.explicitOriginalTarget.activeElement){
return;
}
In Chrome the 'e
' object looks identical when you close the window or click the link.
Is there any way to determine the target in chrome?
3 Answers
Reset to default 5Late response, I know, but you can always try this (confirmed working in IE):
target = document.activeElement;
alert(target.href);
Just showing you that you can grab the active element and then just parse the href to figure out what is happening.
Another option:
$("a").on("click", function(){
window.last_clicked = $(this);
});
Then simply refer to last_clicked
from within your onbeforeunload
handler. This is the best cross-browser patible solution I've found, since document.activeElement
is unreliable.
No. The target of the event is the window or document, not the link. Unlike Firefox, Chrome provides no helpful bonus properties on the event object. Your best bet may be to have an click event handler on the body that examines the event target to see if it's a link, but that's not foolproof: the link may have its own click event handler that prevents the default action, or the user may follow the link using the keyboard, in which case no click event will be fired.
版权声明:本文标题:javascript - How to determine if onbeforeunload has been caused by clicking a link in Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744002095a2574046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论