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.
Related
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 have a asp.net page in which I want to update database when browser close not tab close. I tried window.onbeforeunload event in JavaScript. It is working good enough when browser tab close but not giving desire result when Browser close in firefox only (not IE). What should I do in this circumstance?
I am facing a wired scenario in Firefox 4. When I close the tab window.onbeforeunload is getting called. But when user is closing the window by the cross bar it is not getting called.
Is that a bug in Firefox or there is some other thing I need to do . Please check my code.
$(document).ready(function(){
window.onbeforeunload = function(){signout()};
});
window.onbeforeunload = function(){signout()};
Even writing this code outside document.ready is also not working. The same code is working on chrome.
The scenario get's even wired when their are more than one tabs are opened and then user closes the window (not the tab) by cross bar. In this scenario , Firefox asks the user to confirm that he is closing two tabs and after that my code works !!!. This is quite surprising my code does not work only when my page is the only tab and the user closes the window.
There's a thread on it here:
window.onunload only fires when a tab is closed in firefox, not the entire browser
It looks like it was a feature around that time to prevent unscrupulous websites from preventing browser being fully closed. I wouldn't be surprised if this is still the case, but the only way to find out is to try on a more current version of Firefox.
i'm looking for a reliable way on how to detect when a user closes the browser/tab in order to display a warning message (i'm having a shopping cart which uses sessions).
i've googled and couldn't find a proper solution - window.onunload will display a message every time i'm refreshing the page ..
any ideas?
thanks
You can't tell the difference between closing, reloading, back/forward etc.
beforeunload is fired on all of them.
Depending on if you launched the window yourself, you could run your code before calling window.close(), but this won't be called if the user closes the window themselves.
I don't believe such a thing is possible.
The browser can fire an event when the page (un)loads, but who's to say wether the user is navigating, or closing the browser/tab?
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"