How to Create a Custom Dialog Box in AJAX for HTML5 and CSS3 Programming - dummies

How to Create a Custom Dialog Box in AJAX for HTML5 and CSS3 Programming

By Andy Harris

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.


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">
       The dialog class allows you to have a movable, sizable
       customized dialog box consistent with the installed
       page theme.
  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:

  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
  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(){
     } // end openDialog