Is there an alternative method to use onbeforeunload in mobile safari?

Asked
Active3 hr before
Viewed126 times

6 Answers

method
90%

Read the question before you downvote: "Is there an alternative method to use instead of onbeforeunload in mobile-safari?". the dialog was just an example to test it. – artkoenig Apr 14 '16 at 15:06 ,Is there an alternative method to use instead of onbeforeunload in mobile-safari? I've noticed that Google was able to capture the onbeforeunload event in mobile-safari. Has anyone figured out how they are doing so?, Can sanitary pads be a better alternate to gauze to reduce or stop bleeding? , there is both onunload and onbeforeunload Can you clarify what exactly you're trying to accomplish? – Steve Jul 7 '11 at 0:44

I would guess that they are using the History API. By listening to popstate

window.addEventListener("popstate", function(e) {
   ...
});
88%

Is there an alternative method to use instead of onbeforeunload in mobile-safari? I've noticed that Google was able to capture the onbeforeunload event in mobile-safari. Has anyone figured out how they are doing so?,Google was able to do so using their gmail client. Create a new message... type something in the text area... hit the browser back button. It pops up a warning message. I used an iPad to do my test.,you can show a warning before allowing doing anything stupid.,I would guess that they are using the History API. By listening to popstate

I would guess that they are using the History API. By listening to popstate

window.addEventListener("popstate", function(e) {
   ...
});
72%

The HTML specification states that authors should use the Event.preventDefault() method instead of using Event.returnValue to prompt the user. ,WindowEventHandlers.onbeforeunload, Typically, it is better to use window.addEventListener() and the beforeunload event, instead of onbeforeunload. ,Guarantee the browser unload by removing the returnValue property of the event

window.addEventListener("beforeunload", function(event) {
   ...
});
window.onbeforeunload = function(event) {
   ...
};
load more v
65%

No it does not fire in all browsers. It's not supported in mobile browsers e.g. Safari, Opera Mobile & mini, Dolphin. See Is there an alternative method to use onbeforeunload in mobile safari?,Paste above template to empty file then edit it,I found a workaround for Firefox with setTimeout function because it does not have the same behaviour as other web browsers.,Does window.onbeforeunload() fire in all browsers? I need a onbeforeunload functionality which is supported at least by IE6 and FF3.6.

Building upon Tushar Ahirrao solution this works cross browser and triggers once (Works in Firefox, Chrome, whatever)

<html>
<head>
<script type="text/javascript">
var app = {};
app.unloaded = false;
app.unload = function() {
    if (app.unloaded) return; else app.unloaded = true;
    // your code here
    return "YO";
};
</script>
</head>
<body onunload="return app.unload();" onbeforeunload="return app.unload();">
YO
</body>
</html>
load more v
75%

Only run code if it's on a platform with known beforeunload "issues".,Unfortunately Mobile Safari does not support "beforeunload". The only way I can think of to get partial support would be to "intercept" all possible page exist events on the page (e.g. URL clicks, form submits, etc.) and manually fire off a beforeunload event. There is some discussion on the issue here:,The code does offer some interesting functionality for the defined scenario.,The only other solution I've run across is window.onpopstate, which also is ignored/skilled by Mobile Safari in this situation.

<script src="/scripts/jquery.are-you-sure.js"></script>
<script src="/scripts/jquery.are-you-sure.beforeunload-shim.js"></script>
load more v
40%

use the beforeunload event to warn a user they're going to close your page, but only when it's important,If you want to prompt or warn your user that they're going to close your page, you need to add code that sets .returnValue on a beforeunload event: ,a Set of Promise objects can be useful to control beforeunload ,In the video, I explain a bit about the beforeunload event—which lets you prompt or warn your user that they're about to leave your page. If misused, this can be frustrating for your users—why would you use it? 💁‍♂️ℹ️

window.addEventListener('beforeunload', (event) => {
   event.returnValue = `Are you sure you want to leave?`;
});
load more v

Other "method-undefined" queries related to "Is there an alternative method to use onbeforeunload in mobile safari?"