Run JavaScript code when closing a window or refreshing a page?

Is there a way to run the final JavaScript code when the user closes the browser window or refreshes the page?

I'm thinking of something like onload, but more like onclose? Thank.

I do not like the onbeforeunload method, which always gives a confirmation pop-up window (leave the page / stay on mozilla) or (reload / do not reload to chrome). Is there a way to execute the code silently?

+79
javascript browser
Nov 18 '12 at 19:05
source share
8 answers

Well, I found a working solution for this, it consists of using the beforeunload event, and then returning a null handler. This executes the desired code without a confirmation popup. This happens something like this:

 window.onbeforeunload = closingCode; function closingCode(){ // do something... return null; } 

Hope this helps.

+37
Nov 18 '12 at 23:45
source share

There is both window.onbeforeunload and window.onunload , which are used differently depending on the browser. You can evaluate them by setting window properties in functions or using .addEventListener :

 window.onbeforeunload = function(){ // Do something } // OR window.addEventListener("beforeunload", function(e){ // Do something }, false); 

Usually onbeforeunload used if you need to stop the user from exiting the page (for example, the user works with some unsaved data, so he must save before leaving). onunload not supported by Opera, as far as I know, but you can always install both.

+62
Nov 18 '12 at 19:10
source share

jQuery version:

 $(window).unload(function(){ // Do Something }); 

Update : jQuery 3:

 $(window).on("unload", function(e) { // Do Something }); 

Thanks Garrett

+11
May 12 '16 at 12:41
source share

This documentation recommends listening to the onbeforeunload event and / or adding an event listener in the window.

 window.addEventListener('beforeunload', function(event) { //do something here }, false); 

You can also simply populate the .onunload or .onbeforeunload properties of a window with a function or function reference.

Although the behavior is not standardized in browsers, the function may return the value displayed by the browser when confirming whether to leave the page.

+6
May 14 '16 at 1:36
source share

You can use window.onbeforeunload .

 window.onbeforeunload = confirmExit; function confirmExit(){ alert("confirm exit is being called"); return false; } 
+5
Nov 18 '12 at 19:08
source share

The event is called beforeunload , so you can assign the window.onbeforeunload function.

+3
Nov 18 '12 at 19:07
source share

Sometimes you may need to tell the server that the user is leaving the page. This is useful, for example, for cleaning unsaved images temporarily stored on a server, for marking this user as "offline", or for logging in when they are running in their session.

Historically, you sent an AJAX request to the beforeunload function, however this has two problems. If you send an asynchronous request, there is no guarantee that the request will be executed correctly. If you send a synchronous request, it is more reliable, but the browser will freeze until the request is completed. If this is a slow request, this will be a huge inconvenience for the user.

Fortunately, we now have navigator.sendBeacon() . Using the sendBeacon() method, data is transferred asynchronously to the web server when the user agent is able to do this without delaying the upload or affecting the performance of the next navigation. This solves all the problems with providing analytical data: the data is sent reliably, it is sent asynchronously, and this does not affect the loading of the next page. Here is an example of its use:

 window.addEventListener("unload", logData, false); function logData() { navigator.sendBeacon("/log.php", analyticsData); } 

sendBeacon() supported in:

  • Edge 14
  • Firefox 31
  • Chrome 39
  • Safari 11.1
  • Opera 26
  • iOS Safari 11.4

It is currently not supported:

  • Internet explorer
  • opera mini

Here is the polyfield for sendBeacon () if you need to add support for unsupported browsers. If this method is not available in the browser, it will send a synchronous AJAX request instead.

+3
Sep 19 '18 at 18:52
source share

You can try something like this:

 <SCRIPT language="JavaScript"> <!-- function loadOut() { window.location="http://www.google.com"; } //--> </SCRIPT> <body onBeforeUnload="loadOut()"> 
-2
Nov 18 '12 at 19:08
source share



All Articles