Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if onbeforeunload has been caused by clicking a link in Chrome

The problem is as follows.

onbeforeunload works like a charm in Firefox and has e.explicitOriginalTarget.activeElementthat 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?

like image 551
ZooKeeper Avatar asked Jan 31 '10 20:01

ZooKeeper


People also ask

What triggers Onbeforeunload?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page.

How do I stop Onbeforeunload?

$(window). bind('beforeunload',function() { return "'Are you sure you want to leave the page. All data will be lost!"; }); $('#a_exit').

How do I stop Onbeforeunload event during the page refresh?

For what you are looking for, the best way to have control refreshing the webpage would be with the onKeyDown function. Unfortunately pressing the refresh button directly from your browser will load the DOM again, so technically there's no way to prevent the refresh from this action.


1 Answers

Late 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.

like image 109
SegaAges Avatar answered Sep 30 '22 07:09

SegaAges