Reputation: 7060
Just wondering, is it possible to create an alert with multiple options?
Like for example, in Facebook, when you try to close the tab/window when you have not finished typing your message, an alert with the options "Leave this page" and "Stay on this page" will pop up.
Upvotes: 6
Views: 16616
Reputation: 52
A little late, but you can use the beforeunload event.
window.addEventListener("beforeunload", function (ev) {
ev.preventDefault();
})
Upvotes: 0
Reputation: 1486
Example with form, you'are looking for window.onbeforeunload:
<script type="text/javascript">
var originalFormContent
var checkForChanges = true;
jQuery(document).ready(function () {
originalFormContent = jQuery('#myForm input[type=text]').serialize() + jQuery('#myForm select').serialize();
});
function onClose() {
if (checkForChanges && originalFormContent != "undefined") {
var content = jQuery('#myForm input[type=text]').serialize() + jQuery('#myForm select').serialize();
if (content != originalFormContent) {
return confirm('You have unsaved changes. Click OK if you wish to continue ,click Cancel to return to your form.');
}
}
}
window.onbeforeunload = onClose();
Upvotes: 5
Reputation: 103368
You're referring to window.onbeforeunload
:
Best way to detect when a user leaves a web page?
You can also use the window.confirm()
function for OK
/Cancel
options:
Other than that, you'd have to implement a custom modal alert, such as jQuery Dialog.
Upvotes: 4
Reputation: 63
Please have a look at http://jqueryui.com/dialog/#default
Copied this from a previous answer: JavaScript alert with 3 buttons
Upvotes: 0