Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check user has clicked refresh button or close x

Tags:

jquery

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?

like image 748
pradeep Avatar asked Aug 14 '09 10:08

pradeep


People also ask

How do I know if my refresh button is clicked?

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.

How do I know if my browser is close or refresh?

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.


1 Answers

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?

like image 91
Mad Halfling Avatar answered Nov 07 '22 19:11

Mad Halfling