window.onbeforeunload not working on the iPad?

JavascriptIosIpadOnbeforeunload

Javascript Problem Overview


Does anyone know if the onbeforeunload event is supported on the iPad and/or if there's a different way to use it?

I've tried pretty much everything, and it seems like the onbeforeunload event is never triggered on the iPad (Safari browser).

Specifically, this is what I've tried:

  • window.onbeforeunload = function(event) { event.returnValue = 'test'; }
  • window.onbeforeunload = function(event) { return 'test'; }
  • (both of the above together)
  • window.onbeforeunload = function(event) { alert('test')'; }
  • (all of the above functions but inside <body onbeforeunload="...">

All of these work on FF and Safari on the PC, but not on the iPad.

Also, I've done the following just after loading the page:

alert('onbeforeunload' in window);
alert(typeof window.onbeforeunload);
alert(window.onbeforeunload);

Respectively, the results are:

  • true
  • object
  • null

So, the browser does have the property, but for some reason it doesn't get fired.

The ways I try to navigate away from the page are by clicking the back and forward buttons, by doing a google search in the top bar, by changing location in the address bar, and by clicking on a bookmark.

Does anyone have any idea about what's going on? I'd greatly appreciate any input.

Thanks

Javascript Solutions


Solution 1 - Javascript

This bit of JavaScript works for me on Safari and Chrome on ipad and iphone, as well as desktop/laptop/other browsers:

var isOnIOS = navigator.userAgent.match(/iPad/i)|| navigator.userAgent.match(/iPhone/i);
var eventName = isOnIOS ? "pagehide" : "beforeunload";

window.addEventListener(eventName, function (event) { 
   	window.event.cancelBubble = true; // Don't know if this works on iOS but it might!
    ...
} );

Solution 2 - Javascript

I have found that the onunload() event does fire. It's behavior is somewhat odd; whatever you have in your callback function attached to the event is actually run after the new page has loaded in the background (You can't tell it's loaded yet, but server logging will show that it has).

More oddly, if you have a confirm() call in your onunload(), and the user has clicked a link to go somewhere else, you are in business. If, however, the user closes the iPad Safari browser tab, the onunload() event will fire, but your confirm() will have an implicit cancel as response.

Solution 3 - Javascript

Only Apple would know for sure, but my guess is that they purposely did not enable that functionality in mobile Safari because it is most often used by shady characters to get you to stay on their site or pop up lots of porn/advertising windows.

Solution 4 - Javascript

There's a known bug in WebKit with onbeforeunload. I believe it's fixed in the latest beta of Chrome 5, but it's quite possible the iPad's browser is made from a version of WebKit that doesn't have the fix.

Related Chrome bug report.

Solution 5 - Javascript

beforeunload event is not supported by Mobile Safari. You can see the list of all supported events here: Handling Events Apple documentation

And the beforeunload is not in the list!

Solution 6 - Javascript

https://code.google.com/p/chromium/issues/detail?id=97035

see hear.

> alerts are no longer allowed during page dismissal events (beforeunload, unload, pagehide).

I think alerts, prompt, confirm, and other actions like these are also no longer allowed.

Solution 7 - Javascript

Here's a solution that should work on all modern browsers:

var unloaded = false;
window.addEventListener("beforeunload", function(e)
{
    if (unloaded)
    	return;
    unloaded = true;
    console.log("beforeUnload");
});
window.addEventListener("visibilitychange", function(e)
{
	if (document.visibilityState == 'hidden')
    {
    	if (unloaded)
	    	return;
	    unloaded = true;
	    console.log("beforeUnload");
    }
});

Mobile browsers don't tend to not support beforeunload because the browser can go into the background without unloading the page, then be killed by the operating system at any time.

Most desktop browser contain a bug that causes visibilityState to not get called when the document unloads. See: here.

Therefore, it's important to include both events to cover all scenarios.

NB

I have used console.log instead of alert in my example because alert will get blocked by some browsers when called from beforeunload or visibilitychange.

Solution 8 - Javascript

If you just need to know if the page has been left you can use document.unload. It works fine in ios browsers. If you see on Apple documentation you'll find that it's deprecated and they recommend to use document.pagehide

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArt ZambranoView Question on Stackoverflow
Solution 1 - JavascriptDangerView Answer on Stackoverflow
Solution 2 - JavascriptDanny ArmstrongView Answer on Stackoverflow
Solution 3 - JavascriptCharles BoyungView Answer on Stackoverflow
Solution 4 - JavascriptJoel MuellerView Answer on Stackoverflow
Solution 5 - JavascriptJulien BachmannView Answer on Stackoverflow
Solution 6 - Javascriptzelda.jView Answer on Stackoverflow
Solution 7 - JavascriptDan BrayView Answer on Stackoverflow
Solution 8 - JavascriptMiquelView Answer on Stackoverflow