Tassadaque
Tassadaque

Reputation: 8199

window.onbeforeunload alert in case of click of submit

I have the following code to handle to prompt user in case of unsaved form data but this code also showing alert in case of click of submit button

$(document).ready(function () {
    var _isDirty = false;
    $(":input").live("change", function () {
        _isDirty = true;
    });

    $(':[type=submit]').live('click', function () {
        _isDirty = false;            
    });
    window.onbeforeunload = function () {
        if (_isDirty) {            
            return 'You have made changes to data on this page.  If you navigate away from this page without first saving your data, the changes will be lost.';
        }
        else {           
            return null;
        }
    }
}

Upvotes: 0

Views: 4370

Answers (2)

Chuck
Chuck

Reputation: 21

onbeforeunload has a quirk that if you return anything, even null, it will throw the alert. Try removing the else clause else {return null;}, and it should stop showing up.

Upvotes: 2

MUS
MUS

Reputation: 1450

Follow this post for your answer.

Upvotes: 0

Related Questions