tags:

views:

477

answers:

3

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?

A: 

Hi,

i think both are "unload" events in the Browser. You can try :

$(window).unload(function () { 
    alert("QueSeraSera.. lalalala"); 
});
ArneRie
+1  A: 

The 'window.onbeforeunload' event should capture what you're trying to do. It will, however, also get fired if a POST occurs (i.e. user clicks a submit button) or if the user tries to navigate away from the page. I'm sure jQuery has a nice wrapper for this, but you can find some information on the javascript method itself here:

https://developer.mozilla.org/en/DOM/window.onbeforeunload

Jesse Taber
+1  A: 

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?

Mad Halfling