In this article, I am explaining how to create a Modal Popup using JavaScript. In one of my previous articles I explained How to open a PopUp Window. You can refer the article here.



The concept is to open a PopUp and then freeze the parent window and when the popup is closed the parent window is changed back to normal.


Parent Page

To start with here is the script that will open a PopUp Window.

<script type = "text/javascript">

    var popUpObj;

    function showModalPopUp()



    "toolbar=no," +

    "scrollbars=no," +

    "location=no," +

    "statusbar=no," +

    "menubar=no," +

    "resizable=0," +

    "width=100," +

    "height=100," +

    "left = 490," +








You will notice above that I am calling a function LoadModalDiv() which is given below. The job of this function is to freeze the screen using a DIV.




<script type = "text/javascript">

    function LoadModalDiv()


        var bcgDiv = document.getElementById("divBackground");"block";

        if (bcgDiv != null)


            if (document.body.clientHeight > document.body.scrollHeight)


       = document.body.clientHeight + "px";




       = document.body.scrollHeight + "px" ;


   = "100%";





And finally the third function that will unfreeze the parent page by hiding the Modal DIV. This function will be called from the PopUp page in the page unload event which will trigger when the PopUp page is closed.


<script type = "text/javascript">

     function HideModalDiv()


        var bcgDiv = document.getElementById("divBackground");"none";




For freezing the screen I am using a Transparent DIV whose Z-index is set to value greater than zero so that it overlaps the parent page.


<div   id = "divBackground" style=" position:absolute; top:0px; left:0px;background-color:black; z-index:100;opacity: 0.8;filter:alpha(opacity=60); -moz-opacity: 0.8; overflow:hidden; display:none">




PopUp (Child) Page


In the PopUp Page I have written a small script that runs when the PopUp page is closed.

<script type = "text/javascript">

  function OnClose()


    if(window.opener != null && !window.opener.closed) 





  window.onunload = OnClose;



As you will notice above I am calling the HideModalDiv() function of the parent page when the window unload event is triggered. This will unfreeze the screen as soon as PopUp is closed.


Figure below displays the Demo Modal PopUp Window.

Modal PopUp using JavaScript


View Demo

The above script is tested in the following browsers.

1. Internet Explorer 7

2. Firefox 3

3. Google Chrome

4. Safari 4.


This completes the article. You can download the sample source from the link below. (3.23 kb)