Crossbrowser onbeforeunload?

Asked
Active3 hr before
Viewed126 times

7 Answers

onbeforeunload
90%

I found a workaround for Firefox with setTimeout function because it does not have the same behaviour as other web browsers.,It's my recollection that IE was the only browser to implement onbeforeunload, but some browsers have taken it upon themselves to implement it.,No it does not fire in all browsers. It's not supported in mobile browsers e.g. Safari, Opera Mobile & mini, Dolphin. See Is there an alternative method to use onbeforeunload in mobile safari?, @Aelios, will this cause browsers like Opera to fire the onbeforeunload event before unloading? – KyleMit Nov 3 '14 at 22:28

I found a workaround for Firefox with setTimeout function because it does not have the same behaviour as other web browsers.

window.onbeforeunload = function(e) {
   var message = "Are you sure ?";
   var firefox = /Firefox[\/\s](\d+)/.test(navigator.userAgent);

   if (firefox) {
      //Add custom dialog
      //Firefox does not accept window.showModalDialog(), window.alert(), window.confirm(), and window.prompt() furthermore
      var dialog = document.createElement("div");
      document.body.appendChild(dialog);
      dialog.id = "dialog";
      dialog.style.visibility = "hidden";
      dialog.innerHTML = message;
      var left = document.body.clientWidth / 2 - dialog.clientWidth / 2;
      dialog.style.left = left + "px";
      dialog.style.visibility = "visible";
      var shadow = document.createElement("div");
      document.body.appendChild(shadow);
      shadow.id = "shadow";
      //tip with setTimeout
      setTimeout(function() {
         document.body.removeChild(document.getElementById("dialog"));
         document.body.removeChild(document.getElementById("shadow"));
      }, 0);
   }

   return message;
}
load more v
88%

The onbeforeunload property of the WindowEventHandlers mixin is the event handler for processing beforeunload events. These events fire when a window is about to unload its resources. At this point, the document is still visible and the event is still cancelable. , Typically, it is better to use window.addEventListener() and the beforeunload event, instead of onbeforeunload. ,WindowEventHandlers.onbeforeunload, Note: To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with. Moreover, some don't display them at all.

window.addEventListener("beforeunload", function(event) {
   ...
});
window.onbeforeunload = function(event) {
   ...
};
load more v
72%

Does window.onbeforeunload() fire in all browsers? I need a onbeforeunload functionality which is supported at least by IE6 and FF3.6. ,GitHub: https://github.com/Aelios/crossbrowser-onbeforeunload,For IE, onbeforeunload() seems only to be supported by IE9,No it does not fire in all browsers. It's not supported in mobile browsers e.g. Safari, Opera Mobile & mini, Dolphin. See Is there an alternative method to use onbeforeunload in mobile safari?

I found a workaround for Firefox with setTimeout function because it does not have the same behaviour as other web browsers.

window.onbeforeunload = function(e) {
   var message = "Are you sure ?";
   var firefox = /Firefox[/s](d+)/.test(navigator.userAgent);

   if (firefox) {
      //Add custom dialog
      //Firefox does not accept window.showModalDialog(), window.alert(), window.confirm(), and window.prompt() furthermore
      var dialog = document.createElement("div");
      document.body.appendChild(dialog);
      dialog.id = "dialog";
      dialog.style.visibility = "hidden";
      dialog.innerHTML = message;
      var left = document.body.clientWidth / 2 - dialog.clientWidth / 2;
      dialog.style.left = left + "px";
      dialog.style.visibility = "visible";
      var shadow = document.createElement("div");
      document.body.appendChild(shadow);
      shadow.id = "shadow";
      //tip with setTimeout
      setTimeout(function() {
         document.body.removeChild(document.getElementById("dialog"));
         document.body.removeChild(document.getElementById("shadow"));
      }, 0);
   }

   return message;
}
load more v
65%

window.onbeforeunload() é acionado em todos os navegadores? Eu preciso de uma funcionalidade onbeforeunload que seja suportada pelo menos pelo IE6 e FF3.6.,Com base na solução Tushar Ahirrao, isso funciona em vários navegadores e é acionado uma vez (funciona no Firefox, Chrome, qualquer que seja),Para o IE, onbeforeunload() parece ser apenas suportado pelo IE9,Não, ele não dispara em todos os navegadores. Não é suportado em navegadores móveis, por exemplo. Safari, Opera Móvel e mini, Dolphin. Consulte Existe um método alternativo para usar onbeforeunload no safari móvel?

Encontrei uma solução alternativa para o Firefox com a função setTimeout porque ele não tem o mesmo comportamento que outros navegadores da web.

window.onbeforeunload = function(e) {
   var message = "Are you sure ?";
   var firefox = /Firefox[\/\s](\d+)/.test(navigator.userAgent);

   if (firefox) {
      //Add custom dialog
      //Firefox does not accept window.showModalDialog(), window.alert(), window.confirm(), and window.Prompt() furthermore
      var dialog = document.createElement("div");
      document.body.appendChild(dialog);
      dialog.id = "dialog";
      dialog.style.visibility = "hidden";
      dialog.innerHTML = message;
      var left = document.body.clientWidth / 2 - dialog.clientWidth / 2;
      dialog.style.left = left + "px";
      dialog.style.visibility = "visible";
      var shadow = document.createElement("div");
      document.body.appendChild(shadow);
      shadow.id = "shadow";
      //tip with setTimeout
      setTimeout(function() {
         document.body.removeChild(document.getElementById("dialog"));
         document.body.removeChild(document.getElementById("shadow"));
      }, 0);
   }

   return message;
}
load more v
75%

window.onbeforeunload() se déclenche-t-il dans tous les navigateurs? J'ai besoin d'une fonctionnalité onbeforeunload qui est prise en charge au moins par IE6 et FF3.6.,Pour IE, onbeforeunload() semble seulement être pris en charge par IE9,Comment savoir si un bouton d'actualisation ou de retour du navigateur a été cliqué dans Firefox,/Crossbrowser onbeforeunload?

J'ai trouvé une solution de contournement pour Firefox avec la fonction setTimeout car elle n'a pas le même comportement que les autres navigateurs Web.

window.onbeforeunload = function(e) {
   var message = "Are you sure ?";
   var firefox = /Firefox[\/\s](\d+)/.test(navigator.userAgent);

   if (firefox) {
      //Add custom dialog
      //Firefox does not accept window.showModalDialog(), window.alert(), window.confirm(), and window.Prompt() furthermore
      var dialog = document.createElement("div");
      document.body.appendChild(dialog);
      dialog.id = "dialog";
      dialog.style.visibility = "hidden";
      dialog.innerHTML = message;
      var left = document.body.clientWidth / 2 - dialog.clientWidth / 2;
      dialog.style.left = left + "px";
      dialog.style.visibility = "visible";
      var shadow = document.createElement("div");
      document.body.appendChild(shadow);
      shadow.id = "shadow";
      //tip with setTimeout
      setTimeout(function() {
         document.body.removeChild(document.getElementById("dialog"));
         document.body.removeChild(document.getElementById("shadow"));
      }, 0);
   }

   return message;
}
load more v
40%

Javascript’s window.onbeforeunload event is useful for catching users that try to browse away from your page without having completed a certain action. Modern web applications such as Gmail and WordPress have made good use of this event.,Being a non-standard event originally invented by Microsoft back in the IE4 days, window.onbeforeunload has some real quirks, although thankfully every major modern browser does support it.,Prior to jQuery 4, you couldn’t even bind to $(window).bind('beforeunload') due to a bug that has been fixed.,The right way turned out to be quite easy using native Javascript code (thanks to the Mozilla Doc Center for the working solution).

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

   // For IE and Firefox prior to version 4
   if (e) {
      e.returnValue = 'Any string';
   }

   // For Safari
   return 'Any string';
};
22%

Any unload event handler should be bound to the window object:,The exact handling of the unload event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behavior should be tested on all supported browsers and contrasted with the similar beforeunload event.,Global Ajax Event Handlers ,This method is a shortcut for .on( "unload", handler ).

$(window).unload(function() {
   return "Handler for .unload() called.";
});
load more v

Other "onbeforeunload-undefined" queries related to "Crossbrowser onbeforeunload?"