Friday, August 31, 2007

Creating Modal Dialog With Javascript

I got inspired to write this post after reading Tyler Hall's post on modal dialog over at sitening

Nowadays when a web application does not necessary have to be a case of stuffing data through the browser and shipped to the server for processing, where the clients itself becomes a more active participant in the over all application, designing the web UI sometimes calls for methodologies that erstwhile wasn’t heard of in the browser’s domain.

One of such UI design methods is the modal dialog.

Modal dialog are not new. They have been around in the UI design of desktop application for donkey years.

You most likely interacts regularly with modal dialog in your everyday use of your computer.

A modal dialog, in a desktop application is a box or message that forces you to dismiss it by interacting with its options before you can continue using any other part of the program.

Here is a screen shot of a modal dialog:

Modal dialog is one of such design elements that is finding its foray into web applications, and thanks to JavaScript, it is easily implemented in the browser.
It is also simple.

The principle behind it is this: have a DIV that initially won’t be visible; that will contain the options to be presented via the modal dialog. Then have a javascript written that will make it visible when it is required. Once it is visible, any content beneath it will be “unclickable” by the user.

The code that will implement this can be divided into three parts: The HTML, the CSS and the JAVASCRIPT


The button (or whatever you choose to attach the modal dialog activation unto) that will fire the javascript when clicked:

<input type='button' value='show modal dialog' onclick='overlay()' />

Then the overlay DIV which initially will be invisible:

<div id="overlay">


           <p>You are at liberty on what to use the dialog for.</p>

          <form method=" " action=" ">

          <input type='file'/>

          <input type='submit'/>


          Click here to [<a href='#' onclick='overlay()'>close</a>]




#overlay {

     visibility: hidden;

     position: absolute;

     left: 0px;

     top: 0px;




     z-index: 1000;


     opacity: 0.8;


    -moz-opacity: 0.8;


#overlay div {


     margin: 100px auto;

     background-color: #fff;

     border:1px solid #000;




The first style takes care of the full screen overlay: notice the opacity styles while the second style is applied to the modal dialog.

The margin style:
margin:100px auto;
Ensures the dialog is 100px from the top and it is centralized.


function overlay()
xyz = document.getElementById("overlay"); = ( == "visible") ? "hidden" : "visible";

Put everything together and you have your modal dialog. As you can see, the whole stuff is incredibly simple.

Here is a screen shot before the activation of the modal dialog:

And this, after the button was clicked:


Morry said...


A simple explanation that worked a treat for me.

PakTranslations said...

thank you. this was the simplest and shortest code i could for doing this.

worked like a charm ... thanks again!

Dee said...

Thanks for such a clear and simple example. Really helped a lot!!

Marc Cunning said...

yessss.. you are right..
this is the better way to implement javascript modal dialogs, easy way, and very compact code..
Better yet.. full compatible with the all browsers..
konqueror 3.5 too..

Anonymous said...

Thanks very much!