codedance / jquery.AreYouSure

A light-weight jQuery "dirty forms" Plugin - it monitors html forms and alerts users to unsaved changes if they attempt to close the browser or navigate away from the page. (Are you sure?)
508 stars 145 forks source link

Override the OnBeforeUnload dialog and replace it with my own #64

Closed aleczandra closed 9 years ago

aleczandra commented 9 years ago

Is it possible to override the onbeforeunload dialog and replace it with a custom confirmation dialog? This would be a nice feature. I tried to use the plugin, it works fine, but I need to show our own confirmation dialog.

codedance commented 9 years ago

Yes. This is possible. Under "Advanced Usage", you'll see an example on how to mix in your own logic. In short, you'd set "silent" mode to true, and manage your own events and dialog:

    /*
    *  Mixing in your own logic into the warning.
    */
    $('#my-form').areYouSure( {'silent':true} );
    $(window).on('beforeunload', function() {
        isSunday = (0 == (new Date()).getDay());
        if ($('#my-form').hasClass('dirty') && isSunday) {
            return "Because it's Sunday, I'll be nice and let you know you forgot to save!";
        }
    }
shubhamkundu commented 5 years ago

Hi All,

Is the only answer still relevant for modern browsers? Any help is appreciated.

cristianab commented 5 years ago

Starting with Firefox 44, Chrome 51, Opera 38 and Safari 9.1, a generic string not under the control of the webpage will be shown instead of the returned string.