2

I have the situation that data needs to be reliably sent before browser window closes. My current implementation is to use a synchronous AJAX calls. However that's unlikely to work in the near future because the browsers are deprecating synchronous XHR calls according to https://xhr.spec.whatwg.org/#synchronous-flag

What I'm trying is to replace the ajax call with a fake "img" call, parameterize data to be sent and append it as the image's url query string. It seemed to work so far I tried. I don't really care about the server response so that as long as the request is made and pushed to the wire before browser window is unloaded.

My question is how reliable it is? Has anyone gotten any expeirences?

My other options is to keep the data in a cookie or webstorage and send them on the next request but that's based on the assumption that user will revisit which may not be true in my case.

Thanks.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Shawn
  • 32,509
  • 17
  • 45
  • 74
  • Is my understanding correct - you need to send request on browser tab or browser window close? – degr Feb 10 '15 at 07:54
  • http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser – degr Feb 10 '15 at 07:55
  • The question is mostly asking about inserting a "img" tag which sends the request before tab or window close. – Shawn Feb 10 '15 at 14:00
  • huh. As I know image tag send request as soon as it possible. Using js you can intercept browser window/tab close event, and add image to your dom. But, in this case, I think, better send ajax request. Also, you can add image with no srs attribute, and set this attribute on close. May be it is have sense to send empty alert (alert()) after request. – degr Feb 10 '15 at 14:10
  • Yes I can send ajax requests. I can also capture the window unload event and insert a "img" tag with "src" immediately. I expect the data will be sent successfully but I don't care about the response. My concern is how reliable will the "img" based request be comparing to the ajax way. – Shawn Feb 10 '15 at 22:09
  • Possible duplicate of [JavaScript, browsers, window close - send an AJAX request or run a script on window closing](http://stackoverflow.com/questions/6162188/javascript-browsers-window-close-send-an-ajax-request-or-run-a-script-on-win) – Michał Perłakowski Jan 12 '17 at 10:16

2 Answers2

0

You can do it in unload event of window using ajax

you can refer the following links to know more about the problems and functionalities you need to take care of at this time in following links

Is there any possibility sending request before window closes

Is it reliable?

Is $(window).unload wait for AJAX call to finish before leaving a webpage

Hope this helps

Community
  • 1
  • 1
Frebin Francis
  • 1,905
  • 1
  • 11
  • 19
0

I think better use ajax request. I have no proof, but from my expirience, dom work slowly then js. For an example, when you do this one:

var div = document.createElement('div');
div.innerHTML = "mama";
div.className = "myDiv";
document.getElementById("myWrapper").appendChild(div);
var text = document.getElementByClassName('myDiv')[0].innerHTML;

sometimes you will get exception with message - can't read property innerHTML of undefined. But, if you will do that

    setTimeout(function(){
        var text = document.getElementByClassName('myDiv')[0].innerHTML;
    }, 50);

it work allways fine. It's because dom still not updated. So, when you add image, dom may not be able to process it. And, when you send ajax request, it will be sended in any case I think.

degr
  • 1,559
  • 1
  • 19
  • 37