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"
Related
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
I have a seemingly simple requirement but I have been stuck for days. Can someone give me a hand?
I need a confirmation prompt if the user tried to close the pop-up window
if the user click ok to close, I need to call an ajax call
My original design is to add an onbeforeunload event handler, have it returns a string which triggers a prompt. Works perfectly.
The problem is the next part. Added a unload listener, a pagehide listener, and a visibilitychange listener - in all three cases, Chrome doesn't fire the event if the user close the window, only if I refresh the window. Firefox works perfectly. I am using a sendbeacon call which should work in these scenarios and if I add a breakpoint to pause before the window closes, the beacon is sent, so it seems like Chrome is closing the document too fast and never bother sending the last beacon, which makes the whole exercise pointless.
Has anyone face similar issues and if so, any way to work around it?
I'm struggling with the same problem.
Reading about the event on the documentation I've noticed that it is an unstable event, and moreover in the compatibility table, Chrome is set to "not supported".
But I noticed that chrome fire the event one time only.
If I close the browser and then i re-open it, the first time the event is fired, but it not work with tab closing.
I want an event that work when I close browser only (not close tab or reload page).
I am using the following code :
$(window).on("unload",function(){
localStorage.clear();
});
I have tried onunload & onbeforeunload, it works for close browser, reload page, close tabs, but for me I want an event that affects only close browser. Any recommended event (close browser only) for me.
You can only detect an unload of a page, not closing of the browser. It just cannot be done.
There's no such event that exclusively works on browser close.
I need a java script code for a button click that press alt+f4 key of keyboard.
So I achieve the same functionality as pressing alt+F4.
So please suggest me how I can do that.
That key event is (on most OSs I guess) processed by the OS before it's even sent to the browser, so capturing it inside a browser wont help.
What u want is to replicate the window/tab close event and AFAIK the only way in Javascript to detect that kind of stuffs are onunload & onbeforeunload events.
Unfortunately those events are also fired when you leave a site over a link or your browsers back button.
You can only detect when the page is unloaded, not when the window is closed. Also, the onbeforeunload is non-standard, so it's not supported by all browsers.
I think you are looking for
window.close();
I expected a window.resize event to happen but apparently that is not triggered as in iOS Safari.
Any ideas?
the mobile safari address bar seems independent of the actual page. mobile chrome on android fires a resize event which is a bad idea. the concept of resizing a window does not exist on a single-window interface.