I have to check in my page that page is unloaded by refresh button or close window button, How is it possible using java script / jquery?
Just check if there are values in the cache. If there was a back button then some saved values will still be there, and if the refresh button was clicked then everything will be the default. This is easily done in PHP but can probably be accomplished in Javascript too if you just check for set variables.
A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.
Javascript is
window.unload = <function>
jquery is
$(window).unload(<function>)
or there are also beforeunload events, it depends on what you are trying to do as to the event timing
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "Not leaving page";
}
These will be called when the page is unloaded, by refreshing, closing the window/tab or navigating away from it (either by a link or by replacing the url).
Be aware that some people may have browsers that may filter out certain script events or actions - what was it, exactly, that you were trying to acheive?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With