Fire Onbeforeunload Only When Window Is Closed (includes Working Example)
I want to fire the onbeforeunload event only when the tab or browser is closed and not upon refresh, link click and so on. I found a site where that works: https://checkout.deindea
Solution 1:
For readers that want to use onbeforeunload
,
The event onbeforeunload
should be used for preventing user's data loss, like filled in forms and other data that are not saved and will be lost upon page refresh.
Using onbeforeunload
to prevent a user from leaving your page is annoying and will not stop the user from leaving.
You can not detecta browser tab refresh button press, unless the user use a shortcut that you can detect with key press (like Ctrl+F5). More info here : javascript beforeunload detect refresh versus close
Old answer was removed in order not to mislead anyone (it was not working on nowadays browsers).
More stack overflow links related on onbeforeunload
use:
How can i get the destination url in javascript onbeforeunload event?
Post a Comment for "Fire Onbeforeunload Only When Window Is Closed (includes Working Example)"