Custom Search
Logiclabz
  • Home
  • Javascript
  • Create a Modal Window (DIV Element) with Javascript on HTML

Create a Modal Window (DIV Element) with Javascript on HTML

Concept of creating a modal window in html is follows

  • Create div element with higher zindex with less opacity for masking the body of HTML.
  • Created masking div element should be of same size as of total height and width of body.
  • Modal window (DIV element) can be then created with zindex higher than masking div.

For IE6 select element over absolute div problem, iframe of same size as of masking div is created and placed in between masking div and body element.

Javascript function to create a modal window by masking body in HTML

View Demo

var maskpanel=function()
{
    this.divobj;
    this.show=function()
    {
      if(!document.getElementById("xdivmasking"))
      {
          var divEle=document.createElement('div');
          divEle.setAttribute("id","xdivmasking");
          document.body.appendChild(divEle);
          var divSty=document.getElementById("xdivmasking").style;
          divSty.position="absolute"; divSty.top="0px"; divSty.left="0px";
          divSty.zIndex="46"; divSty.opacity=".50";divSty.backgroundColor="#000";
          divSty.filter="alpha(opacity=50)";

          var divFram=document.createElement('iframe');
          divFram.setAttribute("id","xmaskframe");
          document.body.appendChild(divFram);
          divSty=document.getElementById("xmaskframe").style;
          divSty.position="absolute"; divSty.top="0px"; divSty.left="0px"; 
          divSty.zIndex="45";divSty.border="none";divSty.filter="alpha(opacity=0)";
       }

       this.divobj=document.getElementById("xdivmasking");
       this.waitifrm=document.getElementById("xmaskframe");
       
       var dsh=document.documentElement.scrollHeight;
       var dch=document.documentElement.clientHeight;
       var dsw=document.documentElement.scrollWidth;
       var dcw=document.documentElement.clientWidth;
       
       var bdh=(dsh>dch)?dsh:dch;
       var bdw=(dsw>dcw)?dsw:dcw;

       this.waitifrm.style.height=this.divobj.style.height=bdh+'px';
       this.waitifrm.style.width=this.divobj.style.width=bdw+'px';  
       this.waitifrm.style.display=this.divobj.style.display="block";
    };
    this.hide=function()
    {
        this.waitifrm.style.display=this.divobj.style.display="none";
    };
}

"maskpanel" function to be placed between the "script" in body of HTML.

"maskpanel" function can be used as
	divmask=new maskpanel();
	divmask.show();
After using maskpanel can be closed as
	divmask.hide();

View Demo of "maskpanel" Javascript function to create a modal window by masking body in HTML.


Comments

  • Jon says:
    Aug 03, 09

    I like the simplicity of this - no special effects, just a mask and a content div.

  • mohan says:
    Sep 02, 09

    this is very good. Thanks.

  • mohan says:
    Sep 02, 09

    this is very good. Thanks.

  • perrucho says:
    Sep 08, 09

    The best I have seen! Congratulations!

  • johny says:
    Sep 16, 09

    It certainly looks very elegant. Out of curiosity I tested the code and I found that it works just fine even without calling the show() and hide() method on the javascript object. That is showing and hiding the "demodiv" div alone seems to works just as well. What did I miss? Thanks.

  • Gagan says:
    Nov 12, 09

    perfect solution to create model window I have seen ever before. Thanks a lot.

  • W says:
    Jan 06, 10

    Much the simplest example I've found, and works on IE6! Thanks.

  • Hieronymus P. Organthruster says:
    Jan 15, 10

    To echo comments above, this is by far and away the simplest and yet most complete example of a modal DIV - thank you for taking the time to write and to publish it. There are countless alternatives but 99% of these seem to require heavyweight, lazy boy Javascript libraries to achieve the same effect.

  • Riz says:
    Apr 08, 10

    Yes, really very good many thanks


Leave a reply


Do you like this post?