0

I want to detect the exit event on all browser, I tried this code :

<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function (event) {
    var message = 'Important: Please click on \'Save\' button to leave this page.';
    if (typeof event == 'undefined') {
        event = window.event;
    }
    if (event) {
        event.returnValue = message;
    }
    return message;
};
</script>
</head>
<body>
<p>
Exit your browser .... 
</p>
</body>
</html> 

this code works in Chrome and safari but it it does'nt work in IE 11 and Fireforx 50.0.1 ... Any ideas ?

Mouaici_Med
  • 390
  • 2
  • 19

3 Answers3

1

From the MDN page following notes are being mentioned

Note also, that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.

The hidden keys inside about:config can be found with the keys dom.disable_beforeunload and dom.require_user_interaction_for_beforeunload.

Your code seems fine for the rest, so it may help to look in your config files (The note mentions mobile browsers, however I have these settings on my Desktop browser as well)

Icepickle
  • 12,689
  • 3
  • 34
  • 48
  • thank you for your reponse. I work on software witch will be used by clients poste, so your solution need to do this configuration in all clients posts ? – Mouaici_Med Mar 01 '17 at 10:04
0

Try with window.addEventListener('beforeunload', function(evt){ ...

Dexion
  • 1,101
  • 8
  • 14
0

this is the response that I find, It work for me.

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";

  (e || window.event).returnValue = confirmationMessage; //Gecko + IE
  return confirmationMessage;                            //Webkit, Safari, Chrome
});

thank you

Mouaici_Med
  • 390
  • 2
  • 19