Tip: Click lines to highlight, hold ctrl/cmd to multi-select

z-index fix for internet explorer (11-Feb @ 15:47)

Fixes the z-index bug in ie6 where so called "windowed" elements (select, object, etc) dont respect z-index in relation to "non-windowed" elements.

www.flickr.com-photos-bjarlestam

Syntax Highlighted Code

  1. <script type="text/javascript">
  2.      // make the specified div a windowed control in IE6
  3.      // this masks an iframe (which is a windowed control) onto the div,
  4.      // turning the div into a windowed control itself
  5.      function makeWindowed(p_div)
  6.      {
  7.         var is_ie6 =
  8.            document.all &&
  9.           (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1);
  10.         if (is_ie6)
  11.         {
  12.            var html =
  13.               "<iframe style=\"position: absolute; display: block; " +
  14.               "z-index: -1; width: 100%; height: 100%; top: 0; left: 0;" +
  15.               "filter: mask(); background-color: #ffffff; \"></iframe>";
  16.            if (p_div) p_div.innerHTML += html;
  17.            // force refresh of div
  18.            var olddisplay = p_div.style.display;
  19.            p_div.style.display = 'none';
  20.            p_div.style.display = olddisplay;
  21.         };
  22.      }
  23. </script>
  24. </head>
  25. <div id="test" style="position: absolute; z-index: 2;
  26. top: 50; left: 30; width: 200px; height: 200px;
  27. background-color: red">
  28. &nbsp;
  29. </div>
  30. <select style="position: absolute; z-index: 1; top: 50;">
  31. <option>test</option>
  32. </select>
  33. <a href="javascript:makeWindowed(document.getElementById('test'));">
  34. Make Windowed</a>
  35. </body>
  36. </html>

Plain Code

<html>
<head>
<script type="text/javascript">
     // make the specified div a windowed control in IE6
     // this masks an iframe (which is a windowed control) onto the div,
     // turning the div into a windowed control itself
     function makeWindowed(p_div)
     {
        var is_ie6 =
           document.all && 
           (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1);
        if (is_ie6)
        {
           var html =
              "<iframe style=\"position: absolute; display: block; " +
              "z-index: -1; width: 100%; height: 100%; top: 0; left: 0;" +
              "filter: mask(); background-color: #ffffff; \"></iframe>";
           if (p_div) p_div.innerHTML += html;
           // force refresh of div
           var olddisplay = p_div.style.display;
           p_div.style.display = 'none';
           p_div.style.display = olddisplay;
        };
     }
</script>
</head>
<body>
<div id="test" style="position: absolute; z-index: 2; 
top: 50; left: 30; width: 200px; height: 200px; 
background-color: red">
&nbsp;
</div>
<select style="position: absolute; z-index: 1; top: 50;">
<option>test</option>
</select>
<a href="javascript:makeWindowed(document.getElementById('test'));">
Make Windowed</a>
</body>
</html>

Codedump Run

Permalink: http://codedumper.com/z-index-fix-for-internet-explorer