最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alert with multiple options - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Mar 28, 2014 at 10:17 chris97ongchris97ong 7,0709 gold badges36 silver badges54 bronze badges 3
  • 1 possible duplicate of JavaScript alert with 3 buttons – Andy Commented Mar 28, 2014 at 10:24
  • @Andy But you can't name confirm() options. – chris97ong Commented Mar 28, 2014 at 10:25
  • @Andy Then how did the creator of the Facebook site do it? How did he give the alert two options that have names besides "Ok" and "Cancel"? – chris97ong Commented Mar 28, 2014 at 10:28
Add a ment  | 

4 Answers 4

Reset to default 5

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(); 

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:

http://jsfiddle/UFw4k/1

Other than that, you'd have to implement a custom modal alert, such as jQuery Dialog.

Please have a look at http://jqueryui./dialog/#default

Copied this from a previous answer: JavaScript alert with 3 buttons

A little late, but you can use the beforeunload event.

window.addEventListener("beforeunload", function (ev) {
    ev.preventDefault();
})
发布评论

评论列表(0)

  1. 暂无评论