jQuery UI Dialog: Part 2 – The Modal Dialog

This is part 2 of my series on the jQuery UI Dialog. Part 1 – The Default Dialog covered the most basic usage of the dialog widget. In this second part I will demonstrate a simple modal dialog.

A modal dialog is a dialog that takes focus, and disables the rest of the application until it is closed. They are used to force the user to acknowledge something, or to gather some input. It is worth pointing out that modal dialogs can be annoying for users so you should consider carefully if it is absolutely necessary. User Account Control (UAC) in Windows Vista and Windows 7 is an example of an annoying usage of modal dialogs.

The dialog widget is usually configured by passing the dialog() method an anonymous object containing a set of key value pairs that describe the options required. The anonymous object is created using javascript’s object literal notation. My favourite javascript author, Douglas Crockford, describes the notation:

In the object literal notation, an object description is a set of comma-separated name/value pairs inside curly braces. The names can be identifiers or strings followed by a colon.

A simple code example is:

var myObject = {
    firstProperty: 'value 1',
    secondProperty: 2
};

The options I will use to create a modal dialog are:

height The height of the dialog
modal Boolean indicating if the dialog should be modal
overlay Creates a partially transparent modal overlay layer. Very web 2.0.

View a Demo

And here is the code:

<!-- See part 1 for html preamble -->
<body>  
    <p>Some text on the page.</p>
    
    <div class="make_me_a_dialog">Content of div</div>
    
    <script type="text/javascript">

        $(document).ready(function() {

            $('.make_me_a_dialog').dialog({ bgiframe: true,
                height: 200,
                modal: true,
                overlay: {
                    backgroundColor: '#000',
                    opacity: 0.5
                }                
            });

        });
        
    </script>  
</body>
</html>



jquery ui dialog: Part 1 – The Default Dialog

jQuery UI Dialog This series is going to document increasingly complex usages of the jQuery UI Dialog widget. This first part of the series will demonstrate how to display the default dialog.

Other series members:

jQuery UI Dialog: Part 2 – The Modal Dialog

The jQuery UI Dialog is a powerful client-side dialog control. It is bundled as part of the jQuery UI suite and as such it is available on the Google CDN. Some of its more interesting features are:

  • Modal / non-modal dialogs
  • Support for resizing and dragging
  • Support for buttons
  • Support for Theming
  • Stacking
  • Partially transparent modal overlay layer

The simplest usage of the dialog control is to select a div, and call the dialog() function.

View a Demo

Here is the complete code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Default dialog</title>
    <link href="ui-darkness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
</head>
<body>  
    <p>Some text on the page.</p>
    
    <div class="make_me_a_dialog">Content of div</div>
    
    <script type="text/javascript">

        $(document).ready(function() {

            $('.make_me_a_dialog').dialog();

        });
        
    </script>  
</body>
</html>

Note that the two javascript files, jquery.min.js and jquery-ui.min.js, are loaded from the google content delivery network (CDN). Line 19 selects the div with the class 'make_me_a_dialog' and applies the jquery dialog plugin to it.

 

kick it on DotNetKicks.com