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

Javascript direct confirm() function replacement - Stack Overflow

programmeradmin5浏览0评论

I would like to replace the Javascript confirm() function to allow custom buttons instead of Yes/Cancel. I tried searching but all the solutions are event driven such as jquery dialog(where the code does not wait for a response but it is event driven). Does anyone know of a non-event driven solution. It must work in Safari as well as IE (so no vbscript).

Here is sample code in many parts of my system. This is old code and was not designed with event driven windows in mind. I am trying to avoid a rewrite.

**


// Wait for users response
if (result>2000) {
    if (confirm("Are you sure this is right?")){

       ... do stuff

    } 
 }
 ... continue with other stuff
 ... lots of other code.

 if (confirm("Did you double check your numbers?")){

       ... do more stuff

 } else {
      ... do something
 }

**

I would like to replace the Javascript confirm() function to allow custom buttons instead of Yes/Cancel. I tried searching but all the solutions are event driven such as jquery dialog(where the code does not wait for a response but it is event driven). Does anyone know of a non-event driven solution. It must work in Safari as well as IE (so no vbscript).

Here is sample code in many parts of my system. This is old code and was not designed with event driven windows in mind. I am trying to avoid a rewrite.

**


// Wait for users response
if (result>2000) {
    if (confirm("Are you sure this is right?")){

       ... do stuff

    } 
 }
 ... continue with other stuff
 ... lots of other code.

 if (confirm("Did you double check your numbers?")){

       ... do more stuff

 } else {
      ... do something
 }

**

Share Improve this question asked Nov 14, 2011 at 20:58 user603749user603749 1,7613 gold badges24 silver badges38 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 7

Like the others have said, this isn't possible. confirm is a blocking function - no more script is executed until the user has dismissed the dialog - and you can't simulate that with other methods of Javascript.

A better solution would be to structure your code for asynchronous execution. This is almost always a better idea -- firstly, it lets you decide how your dialogs should look, what buttons there are, etc; and secondly, it doesn't block the user. They might have the important information they need to double-check open in another tab, or elsewhere on the page. With confirm they'd have to answer your question before being able to get to either of these places.

Here's a snippet of what the code might look like. There's a lot of blank bits here, but it might put you on the right track:

if (result>2000) {
    displayConfirm("Are you sure this is right?", {
        "Yes": function () {
            // ... do stuff
        },
        "No": function () {
            // do.. nothing? up to you.
        }
    } 
}

You'll see here that there are two functions defined, but none actually get executed. The displayConfirm function would have to construct a dialog box (in whichever way) and then create buttons, using those functions as the click handlers (or at least, calling them from the click handler).

What you're trying to do is impossible. You'll have to use an event driven custom dialog solution, or stick with the browsers default confirmation dialog.

You will not be able to do this w/o changing your calls. No custom code can stop execution like the confirm box can. Any solution will require editing code to an event model.

As others have mentioned you can't do it directly, but I managed to do it in a round about way. Assuming like me you have a HTML button which submits a form, and want that button to have a jQuery modal dialog...

  1. add an onclick event to the HTML button
  2. make the onlick event open a jQueryUI dialog, and have the onclick event return false to cancel the button's default action
  3. On your jQueryUI dialog, get the Yes/Ok button to remove the onclick event from the button in step 1, and then call then trigger the button to fire the click event
发布评论

评论列表(0)

  1. 暂无评论