beforeunload / unload / pagehide events not raised when closing tab or browser on mobiles (Android and iOS)

I need to send a request to a server when a user leaves a page.

To do so, I listen to the beforeunload, unload and pagehide events on window and send a synchronous  XmlHttpRequest.

window.addEventListener("unload",function(e) {
        var pl = "bla=blabla";
        var req = new XMLHttpRequest();
        req.open("POST","http://myapi/myendpoint",false);
        req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        req.send(pl);
    },false);

    window.addEventListener("beforeunload",function(e) {
        var pl = "bla=blabla";
        var req = new XMLHttpRequest();
        req.open("POST","http://myapi/myendpoint",false);
        req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        req.send(pl);
    },false);

    window.addEventListener("pagehide",function(e) {
        var pl = "bla=blabla";
        var req = new XMLHttpRequest();
        req.open("POST","http://myapi/myendpoint",false);
        req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        req.send(pl);
    },false);

Open in new window


It works fine on desktop.

On mobiles, it works in the following cases:

- if we refresh the page,
- if we leave the page by clicking a link in the page,
- if we enter a new url in the address bar.

But it doesn't work if :
- we close the tab,
- we close the browser.

Tested on Android 4.4.2 (both Chrome 0.39.X and default browser "Internet") and on iOS 8.0.2 (both chrome and Safari).

Why this specific behavior on mobiles and how could I workaround it ?
Thomas FETIVEAUDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James BilousSoftware EngineerCommented:
Unfortunately this is simply unimplemented in most mobile browsers and should not be relied upon. You'll find if you research this problem that it is actually impossible to find a reliable, cross browser compatible  javascript event to handle a tab or browser close on a mobile device and has caused quite a few headaches for developers.

You can check the mobile tab under the "Browser Compatibility" section to confirm my comment.
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload
ironcladsCommented:
Here's an idea.  Perhaps it will work or not.  Since you have little control over the onbeforeunload event on most browsers these days and since all you are trying to do is determine when a user leaves, you can do a periodic "ping" to a server by writing a session id to the server periodically.  This way, after time has elapsed and there isn't any more "pings" coming from the client, that is when the user left the page.  A server based agent can collect the information and determine when the client was no longer on the page.  Just a thought.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas FETIVEAUDeveloperAuthor Commented:
We are going to implement a similar workaround. Thank you both for your answers.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.