DJ WIce said:
In MSIE this script works to get the highest index:
if(document.all)
{
topZ = 0;
for (z=0;z<document.all.length;z++)
{
oldEl = document.all(z);
topZ = Math.max(oldEl.style.zIndex,topZ)
}
topZ++;
}
No it doesn't. It only looks for the z-index set in the style attributes.
It won't find those set useing CSS rules, e.g.
<style type="text/css">
#higher {z-index: 1000000000;}
</style>
To find the actual z-index of an element, you need a browser that
gives access to that. Currently, the ones that I know of are: IE
(proprietary method), Mozilla and Opera 7.2)
Since document.all is MSIE only I would like to know the Mozilla equivalent
of it so the script will work on NN etc.
The problem is not the document.all, but the accessing of the style at all.
You should also know that not all z-indices means the same. An element with
a z-index creates a new stacking context, and elements inside that are
all in ralation to that element, not the rest of the document.
Example:
<div style="position:relative">
<div style="z-index:4; border:5px solid red;width:100px;height:100px;
position:absolute;left:0px;top:0px;">
<div style="z-index:100000;border:5px solid green;width:100px;height:100px;
position:absolute;left:25px;top:25px;"></div>
</div>
<div style="z-index:5; border:5px solid blue;width:100px;height:100px;
position:absolute;left:50px;top:50px;">
<div style="z-index:1; border:5px solid yellow;width:100px;height:100px;
position:absolute;left:25px;top:25px;"></div>
</div>
</div>
The correct stacking here is red<green<blue<yellow, even though the actual
values of the z-indices are ordered as yellow<red<blue<green.
That is because the entire element with red border, including its inner
green div, has z-index 4 in relation to the blue element. The z-index of
100000 is only in relation to the stacking context of the red element.
You don't have to worry about this, though, since too high a z-index is not
a problem.
So, for your code, the direct translation of document.all would be
document.getElementsByTagName("*") . You will probably want to use both:
---
var allElems = document.getElementsByTagName?
document.getElementsByTagName("*"):
document.all; // or test for that too
var maxZIndex = 0;
for(var i=0;i<allElems.length;i++) {
var elem = allElems
;
var cStyle = null;
if (elem.currentStyle) {cStyle = elem.currentStyle;}
else if (document.defaultView && document.defaultView.getComputedStyle) {
cStyle = document.defaultView.getComputedStyle(elem,"");
}
var sNum;
if (cStyle) {
sNum = Number(cStyle.zIndex);
} else {
sNum = Number(elem.style.zIndex);
}
if (!isNaN(sNum)) {
maxZIndex = Math.max(maxZIndex,sNum);
}
}