Vishnudev K
Vishnudev K

Reputation: 2934

How to get the reason for onUnloadEvent getting triggered

In Html page when I am closing the browser I am showing message using

window.onbeforeunload = confirmClose;
                function confirmClose(event){}

But the same event is getting triggered for the following conditions also
http://msdn.microsoft.com/en-in/library/ie/ms536973%28v=vs.85%29.aspx

  • Close the current window.
  • Navigate to another location by entering a new address or selecting a Favorite.
  • Click the Back, Forward, Refresh, or Home button.
  • Click an anchor that refers the browser to another document.
  • Invoke the anchor.click method.
  • Invoke the document.write method.
  • Invoke the document.open method.
  • Invoke the document.close method.
  • Invoke the window.close method.
  • Invoke the window.open method, providing the possible value _self for the window name.
  • Invoke the window.navigate or NavigateAndFind method.
  • Invoke the location.replace method.
  • Invoke the location.reload method.
  • Specify a new value for the location.href property.
  • Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.

How can i get what is the reason for triggering the method?

Upvotes: 1

Views: 31

Answers (1)

RoToRa
RoToRa

Reputation: 38420

No, generally you can't. The closest you can get is, if the action that triggers the close event also triggers another event, such as clicking on a link, then you can catch that event and set a flag. For example, for clicking on a link (using jQuery):

var linkHasBeenClicked = false;

$('a').click(function() {
  linkHasBeenClicked = true;
});

function confirmClose(event){
  if (linkHasBeenClicked) {
     // ...
  } else {
     // ...
  }
  linkHasBeenClicked = false;
}

Upvotes: 2

Related Questions