tags:

views:

41

answers:

1

We would like to throw a specific message before a user closes the browser. Is there a mechanism which will work across all major browsers (like IE, firefox, chrome, opera)

+3  A: 

Check onbeforeunload:

window.onbeforeunload = function (e) {
  var e = e || window.event;

  if (has_message_to_throw) {

    // For IE and Firefox
    if (e) {
      e.returnValue = 'Specific message';
    }

    // For Safari
    return 'Specific message';

  }


};
KennyTM
I tried the following function, but it doesn't work for IE 8, latest version of Opera. What am I missing here?window.onbeforeunload = function (e) { var message = "Your confirmation message goes here.", e = e || window.event; // For IE and Firefox if (e) { e.returnValue = message; } // For Safari return message;};
Samuel
@Samuel: Looks like Opera doesn't support `onbeforeunload` (Ref: http://www.zachleat.com/web/2008/04/22/dont-let-the-door-hit-you-onunload-and-onbeforeunload/).
KennyTM
IE8 also didn't work and that is quite important for us.
Samuel