HTML5 and CSS3 All-in-One For Dummies
Book image
Explore Book Buy On Amazon

AJAX lets HTML5 and CSS3 programmers make custom dialog boxes. JavaScript supplies a few dialog boxes (the alert and prompt dialog boxes), but these are quite ugly and relatively inflexible. The jQuery UI includes a technique for turning any div into a virtual dialog box. The dialog box follows the theme and is resizable and movable.

image0.jpg

Building the dialog box is not difficult, but you need to be able to turn it on and off with code, or it will not act like a proper dialog box (which mimics a window in the operating system):

  1. Create the div you intend to use as a dialog box.

    Create a div and give it an ID so that you can turn it into a dialog box node. Add the title attribute, and the title shows up in the dialog box's title bar.

      <div id = "dialog"
       title = "my dialog">
      <p>
       The dialog class allows you to have a movable, sizable
       customized dialog box consistent with the installed
       page theme.
      </p>
      </div>
  2. Turn the div into a dialog box.

    Use the dialog() method to turn the div into a jQuery dialog box node in the init() function:

      $("#dialog").dialog();
  3. Hide the dialog box by default.

    Usually you don't want the dialog box visible until some sort of event happens. In this particular example, you may not want the dialog box to appear until the user clicks a button. You can put some code to close the dialog box in the init() function so that the dialog box will not appear until it is summoned.

  4. Close the dialog box.

    To close a dialog box, refer to the dialog box node and call the dialog() method on it again. This time, send the single value “close” as a parameter, and the dialog box will immediately close:

      //initially close dialog
      $("#dialog").dialog("close");
  5. Clicking the X automatically closes the dialog box.

    The dialog box has a small X that looks like the Close Window icon on most windowing systems. The user can close the dialog box by clicking this icon.

  6. You can open and close the dialog box with code.

    My Open Dialog and Close Dialog buttons call functions that control the behavior of the dialog box. For example, here is the function attached to the Open Dialog button:

     function openDialog(){
      $("#dialog").dialog("open");
     } // end openDialog

About This Article

This article is from the book:

About the book author:

Andy Harris taught himself programming because it was fun. Today he teaches computer science, game development, and web programming at the university level; is a technology consultant for the state of Indiana; has helped people with disabilities to form their own web development companies; and works with families who wish to teach computing at home.

This article can be found in the category: