Search code examples
javascriptjqueryonbeforeunload

jquery onbefore unload trigger


I'm using a script to fade out the page content when user is actually leaving the page.

For that I tried using the beforeunload event.

It works fine when I navigate through my site, however I also found it triggering on application launch, such as skype button. Even though I never left the page yet...

What is the best way to validate onbeforeunload event against premature triggering?

I guess I should still use my:

$(window).one('beforeunload', function() {
  // need to make some condition, if really leaving the page - execute
  $('html.nojs').stop(true,false).css('overflow','hidden').animate({opacity:0},2000);
});

But I would need to use some condition... just cannot think of any...

EDIT:

Rmoved the link to the site

The animation does work, but if you go to any specific product and click skype button there you will see the it makes poo...

EDIT2:

The solution to this problem is to detect what triggered an before unload event. As I said, by writing some condition inside that call.

We must find a way to find out whether the event was triggered by external application call (such as skype button that tries to open application) or was it something else, like... for example:( link click, a button submit, script for location change, starting a search, hitting back/forward, or refreshing the page).


Solution

  • This the accepted answer for this question

    Capture user response when using window.onbeforeunload

    If you need to know what triggered the onbeforeunload event, you can have a global variable, then set it to true when you click on the Skype button. Then check it inside your onbeforeload event.