JavaScript Object Help

Here is my object:

function formWatcher(form, options) {

    this.form = $(form); console.log(this.form);
    this.submitted = false;
    this.formcontents = this.form.serialize();
    this.options = {
        "formMessage" : "If you leave this page now, your changes will not be saved.",
        "ajaxMessage" : "This page is still processing your request.",
        "requireSubmit" : false,
        "exempt" : []
    };

    this.form.bind('submit', function() {
        submitted = true;
    });

    $.each(this.options["exempt"], function(e_index, e_value) {
        this.exemptElement(e_value);
    });

    $(window).unload(function(){
        this.confirmExit(this);
    });

    this.exemptElement = function(element) {
        if(element) {
            element.click(function() {
                this.submitted = true;
            });
        }
    };

    this.confirmExit = function(ev) {
        this.newcontents = this.form.serialize();

        if (((this.formcontents != this.newcontents) || this.options.requireSubmit) && !(this.submitted)) {
            ev.stop();
            ev.returnValue = this.options.formMessage;
        } else {
            return true;
        }
    };
};

Now when I attempt to leave the window, I receive “TypeError: this.confirmExit is not a function”.

Please help!

“this” takes on a different value when you’re inside a callback. You’ll need to set a local variable to keep the value of your new formWatcher object.

    [COLOR="#FF0000"]var thisFormWatcher = this;[/COLOR]

    $(window).unload(function(){
        [COLOR="#FF0000"]thisFormWatcher[/COLOR].confirmExit(this);
    });