Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Alert Dialogs

Tags:

jquery

I'm quite new on jquery and i was looking for something that could replace the confirm dialog. I found jQuery Alert Dialogs at http://abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/#demo but jConfirm do not return the same values as confirm(). Is possible to write a function to get the same confirm() value? It's about the meaning of callback function that I admit is not so clear for me :-)

like image 989
Antonio Avatar asked May 16 '11 14:05

Antonio


People also ask

How can use alert in jQuery?

In this article, we will learn how to create an alert message in jQuery when a button gets clicked. For this, we will use the jQuery click() method. jQuery CDN Link: We have linked the jQuery in our file using the CDN link. Example: Create a new HTML file and add the following code to it.

What is dialog in jQuery?

A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default. jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.

What is confirm in jQuery?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

What is a jQuery modal window?

JQuery Modal is an overlay dialog box or in other words, a popup window that is made to display on the top or 'overlayed' on the current page. It is a pop up modal box that is converted into the viewport deliberately for the user to interact with before he can return to the actual site.


1 Answers

jConfirm will never "return" anything because it's "event driven".

jConfirm waits until the user has made a decision and then it will call the callback function which will handle the answer. But jConfirm will not block the code execution flow like the standard confirm(...) does.

So if your previous code looks like this:

// ask for a decision
var answer = confirm("Leave website?"); // the whole script stops until the user has made a decision!

// process answer
if (answer){
    alert("Bye bye!"); // the script waits here until the user has clicked "ok"
    window.location = "http://www.google.com/";
}
else{
    alert("Thanks for sticking around!"); // the script waits here until the user has clicked "ok"
}

it should look like this in jQuery:

// previous code

// show confirm dialog but the script will not wait, the script execution goes forward

jConfirm('Leave website?', 'Confirmation Dialog', function(answer) {

    // this is the callback function of the jConfirm dialog we made
    // we arrive here when the user has made a decision

    // the answer is true, he wants to leave
    if (answer){

      // show a jAlert box
      jAlert("Bye Bye!", "Some Title", function() {

        // this is the callback function of the jAlert box
        // we arrive here when the user has clicked "ok"

        // send him to google
        window.location = "http://www.google.com/";
      });

    }
    else{
      // show a jAlert box without any callback
        jAlert("Thanks for sticking around!", "Some Title");
    }

});

// the code that follows here will be immediately executed
// because unlike confirm(), jConfirm() will not block
// the code execution flow 

And for illustration:

The standard JavaScript confirm() execution flow

  |
  |
  |
  \/
  confirm() waits for an answer... 
  no further code will be executed
  until the user has made a decision
  |
  |
  \/
  handle the user's answer
  |
  |
  \/
  further code 
  execution

The jConfirm execution flow

  |
  |
  \/ -------> jConfirm Dialog  
  |                 |
  |                 |
  |                 \/
  |             Callback function
  |             when the user has made
  |             a decision. 
  |             (handle the answer here)
  |                  
  |
  \/
 further code
 execution
like image 200
sled Avatar answered Nov 14 '22 23:11

sled