San Kap
San Kap

Reputation: 11

window.onbeforeunload doesn't triggered at once

I use a jQuery Dirty Forms plugin in my web-application like this:

$(document).ready(function(){
 $('form.checkUnsavedData').dirtyForms();           
});

$(window).on('beforeunload', function(){    
    if($.DirtyForms.isDirty()){             
        return($.DirtyForms.message);                               
    }           
});

But first click by any link doesn't trigger onbeforeunload event. Only the next clicks trigger it. This problem keeps in different browsers (google chrome, firefox, ie10). What's the matter?

Upvotes: 1

Views: 632

Answers (2)

NightOwl888
NightOwl888

Reputation: 56909

What's the matter?

Dirty Forms automatically attaches to the beforeunload event. You are doing it again. This is likely causing your problematic behavior. You should change your code to just...

$(document).ready(function(){
  $('form.checkUnsavedData').dirtyForms();           
});

Upvotes: 0

Samuel Lopez
Samuel Lopez

Reputation: 2390

I believe you will need to add the onBeforeUnload API As it says in the drupal Download & extend or else you should try not to bind the beforeunload through jQuery and do it with normal javascript like: window.beforeunload = function(){/*code*/};. Finally check what jQuery version are you using, what browsers and how they handle this event. Check out Catching the Javascript beforeunload event, the cross-browser way it's a little dated but it may help you.

Upvotes: 1

Related Questions