1

Sorry for asking stupid question but I really need to trigger this event..

In jQuery I have:

$(document).ready();

Which will get call when DOM is loaded.

Now I want to call function when DOM is closed or its no longer in use or some other DOM loaded instead of current DOM.

So for that, I'm using following event but it's not getting triggered when I'm navigating to some other page.

$( window ).unload(function() {
    alert( "Handler for .unload() called." );
});

Can anyone point me in right way?

Ivan Seidel
  • 2,394
  • 5
  • 32
  • 49
Kalpit
  • 4,906
  • 4
  • 25
  • 43
  • possible duplicate of [Intercept page exit event](http://stackoverflow.com/questions/1704533/intercept-page-exit-event) – moonwave99 Feb 06 '14 at 11:46
  • https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload – piddl0r Feb 06 '14 at 11:47
  • This really depends of what you want to do. Basically, except prompting user with a message, you cannot do anything using onbeforeunload event – A. Wolff Feb 06 '14 at 11:49
  • @A.Wolff i just want to remove css classes when users navigating to some other page – Kalpit Feb 06 '14 at 11:50
  • @piddl0r will it work with all browsers or only with firefox? – Kalpit Feb 06 '14 at 11:50
  • 3
    @user3164335 you cannot do it and anyway as page is changed, seems quite useless. Some browser will let you do it, some don't. You have to test it – A. Wolff Feb 06 '14 at 11:51
  • 1
    @user3164335 browser support is a little iffy. Have a look at this question http://stackoverflow.com/q/14645011/166716 – piddl0r Feb 06 '14 at 11:55

1 Answers1

3

Try with onbeforeunload event

window.onbeforeunload = function() {
    return 'Handler for .unload() called.';
}

In case of latest versions of FF the custom message will not be shown which is a bug. You can find more references in this Question

Community
  • 1
  • 1
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • 1
    No more sure as for lastest FF version but FF doesn't allow any custom message. Anyway +1 because this is the way to do it, returning a string from handler – A. Wolff Feb 06 '14 at 11:50
  • I am not sure about latest versions of FF like you said but its working on chrome and safari etc.. i will check in firefox... Anyway thanks for the info – 웃웃웃웃웃 Feb 06 '14 at 12:03