Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing GET request before leaving page

If a GET request is made as follows

$(window).bind('beforeunload', function() {
    // GET request
});

and the page is abandoned before the GET request is completed,

will the destination server still process the request? Or will it somehow vanish?


I would like to send a server data on beforeunload firing, but without stealing useless ms from the user.

It would be very useful if someone could help me.

like image 979
RadiantHex Avatar asked Jul 23 '10 18:07

RadiantHex


People also ask

What JavaScript event do we use to make the user confirm before leaving the page?

If you want to show confirmation dialog before user leaves or reloads the page, you can use Javascript onbeforeunload() event attribute. When unload event fires, it shows a prompt asking whether user wants to leave or reload the page.

What is beforeunload event in JavaScript?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.


2 Answers

If it is an asynchronous request then the server may process it (if it receives the request) but I don't know if you can guarantee that the request will go through before the page is unloaded or if it will be processed - this may depend on the actual web server (someone else may have more information). If you make a synchronous request, the page will wait until the request goes through and it gets back a response (so in this case, processing is guaranteed). However, this means that your browser will be locked up until that request completes, which may not be desirable.

like image 98
Vivin Paliath Avatar answered Oct 18 '22 12:10

Vivin Paliath


In most cases yes, but it depends on the web application server. Some can detect the disconnect and stop with the request.

like image 35
Joe Martinez Avatar answered Oct 18 '22 14:10

Joe Martinez