开发者

window.onbeforeunload alert in case of click of submit

开发者 https://www.devze.com 2023-03-11 00:58 出处:网络
I have the following code to handle to prompt user in case of unsaved form databut this code also showing alert in case of click of submit button

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 nul开发者_高级运维l;
        }
    }
}


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.


Follow this post for your answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消