How to use JS detect user close page on Safari - javascript

I want to detect user close my page using js, in Chrome/Firefox I use onbeforeunload event, but in Safari (on macOS and iOS) it doesn't work (but MDN say It work...)
In Safari on macOS, onbeforeunload only work first time refresh/close page.
I try unload, beforeunload, pagehide also not work.

Please try the following.
window.unload = function ()
{
// add code here
};
Here is some documentation https://www.w3.org/TR/DOM-Level-2-Events/events.html

Related

Popstate not triggering on Chrome for Android when clicking the back button

Upon clicking the back button in the browser, I would like to prevent the default behaviour of going one page back and instead do an action. I'm using the "popstate" event listener. The following function (I'm using Vue 2) works in all major browsers and even in Firefox for Android, but when I test it in Chrome for Android, it simply goes back one page without popstate being triggered at all.
mounted() {
history.pushState(null, null, <current-url>);
window.addEventListener("popstate", () => { alert(1) });
}
I tried wrapping the popstate event inside the load event and giving it a timeOut of 0, but it still didn't work specifically in Chrome for Android. The version I'm testing on is 93.
I did some more research and it seems that Chrome won't let you use popstate if there is no user interaction first. As long as you click on something or scroll down on mobile, popsate will work, otherwise it won't. I tried to simulate user interaction with click(), but that didn't work either. It seems Chrome wants genuine user interaction. I also realized this is sort of a duplicate of: Chrome popstate not firing on Back Button if no user interaction

Check if user is leaving the page js

I tried onbeforeunload event but it's not working when leaving the page it's only working when I reload but every thing works fine on edge.
I think Chrome have changed somethings in configuration.
Is there a way to fix that or another method?
onbeforeunload = function(){
return("bye");
}
AND is there a way I can check if user is closing the page or redirecting to another url with out firing on reloading or can I not do that?
Even when using JQuery, a framework with the objective cross browser compatibility, they say:
The exact handling of the unload event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behavior should be tested on all supported browsers and contrasted with the similar beforeunload event.
Source
So you have to accept an inconsistent behaviour.
You could try doing this:
window.onbeforeunload = function(e) {
return "Bye";
}
As you need to attach the event to the window

ajax call in window.unload event of IE issue

when a user closes their browser or refresh the browser(onbeforeunload event does NOT fit for my scenario, seems user may cancle to leave), I want to send a log to server, so I have the following code, it works fine in chrome and FireFox, but NOT work in IE8+:
window.onunload = function(){
$.ajax({url:"http://localhost:8888/log",async:false})
};
Try using onbeforeunload event
https://developer.mozilla.org/en-US/docs/DOM/window.onbeforeunload

Why is jQuery unload not working in chrome and safari?

unload function in jQuery works fine in Firefox but not in chrome and safari. please check this fiddle in chrome and Firefox. http://jsfiddle.net/jeevankk/Gywnw/2/ . Alerts a message when the page is refreshed.
$(window).unload(function() {
alert("Unload");
});​
This should work to show a confirmation when the users leaves, this is also not part of any standard.
$(window).on('beforeunload ',function() {
return 'Are you sure ?';
});
I found Joseph's comment as the correct answer, So posting this answer.
Dialogs are blocked/prevented during "beforeunload" (with exception to the beforeunload prompt) and "unload" events. Can be confirmed by checking your console.
This is because the unload event is not part of any standard
https://developer.mozilla.org/en/DOM/window.onunload
check the bottom of the page i just linked to.
jQuery's unload works well in chrome too, with the exception that Chrome doesn't allow alerts within it. I've used it to set cookies. And if it works with Chrome hope it works in Safari too.
the unload function of jquery has some problem with browsers..refer the following link
http://bugs.jquery.com/ticket/5538
can you elaborate on the problem so that we can find some work around??
you can use onfocusout on the body .. but i wouldn't recommend if you are trying to use something like an alert, on this operation, asking the user not to leave your page ..
"refresh" action in the Firefox does not fire the unload event.
We should use onbeforeunload instead.
Confirm with Firefox version 47, Mac OS X

JS event to see if Mobile Safari has been closed?

Is there a JS event that gets triggered if a user closes Safari on their iPhone? onunload works if a person leaves a page, but is not called when the browser is closed.
My hunch is, no, there is no event triggered, as closing Mobile Safari merely 'minimizes' it rather than actually closes it. Can anyone confirm?
Not exactly, but look in the comments for the pagehide event: What event fires when a WebKit WebApp is "terminated"

Categories

Resources