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?)
507 stars 145 forks source link

Firing custom method instead of the alert #68

Open AlainPilon opened 9 years ago

AlainPilon commented 9 years ago

Since the standard alert box is so ugly, it would be nice for the library to allow us to pick a custom function for fire instead of the alert.

codedance commented 9 years ago

Unfortunately this is not possible. All current browsers to my knowledge only support the "standard dialog" for message passed back on the beforeunload event. It is possible to display your own dialog if you're navigating away from the page via a link, but not if you're navigating via the back button, manually typing in a URL, or closing a tab.

If you're in control of the navigation, you could wire up your own code quickly. e.g.:


// Turn on silent mode.
$('form').areYouSure( {'silent':true} );

// In your own page exit code
if ($('form').hasClass('dirty')) {
    // Show your custom fancy dialog
}

Hope this helps.

dyentite commented 9 years ago

So its currently not possible to fire for example, a Bootstrap Modal to use as confirmation instead of the standard alert box?

codedance commented 9 years ago

@dyentite You can do this using the code example above on your own "page exit" event. If you want to use/leverage the browser's event (e.g. closing a tab/window), you'll need to use the browsers own dialog. (This is the same way Gmail works).

kavehmb commented 8 years ago

I'm showing a "please wait " modal after any link is clicked. the problem is that after the user push the STAY button this button stills on the screen, how can I trigger a method to hide my modal by pushing stay button? is there any way to customize the the cancel/stay button behaviour?