Call URL before closing browser window

I want to call the url in the unload-Function of the webpage, but in the unload function, the receive request does not work. My code is as follows:

$(window).unload( function () { jQuery.get("someurl") } ); 

We want to get information about closing the window for some logging of user actions. Is it possible somehow?

If I add a warning () after jQuery.get (), Get-Request will have enough time to do this, but that is not what we prefer.

+4
source share
2 answers

Usage: -

 jQuery.ajax({url:"someurl", async:false}) 
+14
source

You need to use the onbeforeunload event directly. In this case, you can disable the ajax call. This is one place where jQuery is not very useful (yet). The couple notes:

Do not return anything from the onbeforeunload event, or the browser will display the user "Are you sure you want to leave this page."

If you synchronize ajax calls, it will stop page unloading while it is working. If you make it asynchronous, the page will change during the execution of the call (you simply cannot have javascript event handlers for this call). Since jQuery hooks up event handlers, its ajax support is not so useful here.

 window.onbeforeunload = function() { var URL = 'someplace'; var request = null; if (window.XMLHttpRequest){ request = new XMLHttpRequest(); } else if (window.ActiveXObject) { request = new ActiveXObject("Microsoft.XMLHTTP"); } if (request) { request.open("GET", URL, false); request.send(); } } 
+2
source

All Articles