views:

41

answers:

2

How do I determine if onbeforeunload was caused by clicking the close button or a page refresh or generally what event caused onbeforeunload?

here is a snippet:

window.onbeforeunload = function( e ){
    if( !e ) e = window.event;

    // insert code to determine which is the source of the event
}

Please help me.

+1  A: 

As far as I know, there is no way of determining what caused the onbeforeunload. The event is triggered when window is about to close whether closing the browser or some other way.

Sarfraz
A: 

If the close button was pressed the value of e.clientY is negative. For the other possible sources i doubt there is a solution.

window.onbeforeunload = function() {
   var e = window.event;
   alert(e.clientX + " / " + e.clientY);
}
john_doe
Is it? I'm not sure if all browsers register the mouse position once it leaves the "canvas". Also it's possible to "click" the close button with the keyboard in most operating systems.
RoToRa
@RoToRa: The TO has selected the "internet-explorer" tag, so i'm assuming he is looking for an ie-specific solution. And your right. If an shortcut or anything else but the mouse was used to close the window, there won't be any solution at all.
john_doe