jQuery, onBeforeUnload and ASP.Net form

I have the following solution for stopping the users from accidentally leaving my registration page:

var warning = true;

function CheckExit() {
    if (warning) {
        return "Changes done. Remember to save, blah blah.";
    }
}
window.onbeforeunload = CheckExit;

addToPostBack = function (func) {
    var old__doPostBack = __doPostBack;
    if (typeof __doPostBack != 'function') {
        __doPostBack = func;
    } else {
        __doPostBack = function (t, a) {
            if (func(t, a)) old__doPostBack(t, a);
        }
    }
};

$  (document).ready(function () {

    $  ('#aspnetForm').submit(function () {
        warning = false;
        return true;
    });

    addToPostBack(function (t, a) {
        warning = false;
        return true;
    }); 

});

Most of this code is from different questions here on StackOverflow, and works just fine on my local debugging page; all ASP.Net controls (linkbuttons, buttons) can be clicked without a warning, but as soon as the user tries to close the window or navigate away, the warning is displayed.

However, when deployed to my server, the warning pops up when I click ASP.Net controls too (and I really do not want to warn the user that his changes isn’t saved when he clicks the “Save” button). Now, there is a difference between my local debugging page and the server: the server uses a different MasterPage with some ads in it, but after debugging with Firebug (see next paragraph), I can’t see how that should make a difference.

I have tried running different parts of the Javascript code by using Firebug to confirm that everything is loaded correctly (my first guess was that the section containing $ (‘#aspnetForm’) wasn’t being loaded, since this seems to be the difference), but there is no improvement at all. The warning still pops up on every click of a link or a button.

My main problem is, I think, that I do not fully understand what the addToPostBack function does, and as such, I cannot properly debug it.

Is there anybody here who has any idea what might be wrong?

newest questions tagged jquery – Stack Overflow

About Admin