Introducing DOCJSLIB 3.0, The HighestZ Function - Doc JavaScript | WebReference

Introducing DOCJSLIB 3.0, The HighestZ Function - Doc JavaScript


The highestZ() Function

DOCJSLIB Version 2.0 supported the setZposition() function, which sets the Z index of a DHTML element. Version 3.0 adds another Z-index-related function, the findHighestZ() function. This function computes the highest Z index of all of the page's elements. This function is useful when there is no way to keep track of the highest Z index in any other way. The setZposition() function may change the Z index of elements, and is not responsible for keeping track of the highest Z index of the page. When a DHTML element is supposed to be on top of every other element on the page, the highest Z index needs to be computed. Here is the function:

function docjslib_findHighestZ() {
  var documentDivs = new Array();
  if (NS4) {documentDivs = document.layers}
  else {documentDivs = document.all.tags("DIV")};
  var highestZ = 0;
  for (var i = 0; i < documentDivs.length; i++) {
     var zIndex = (NS4) ? documentDivs[i].zIndex : 
       documentDivs[i].style.zIndex;
         // (The above two lines should be joined as one line.
         // They have been split for formatting purposes.)
     highestZ = (Zindex > highestZ) ? Zindex : highestZ;
  }
  return highestZ;
}

The function starts with initializing the documentDivs array:

var documentDivs = new Array();

This array includes all DHTML elements of the page. Netscape Navigator's DHTML elements are identified by the layers array (documentDivs = document.layers). Internet Explorer's DHTML elements are identified by the DIV tags. All DIV elements are collected into the documentDivs array by:

documentDivs = document.all.tags("DIV")

We then loop over all array elements and compare their Z index with the highest one found so far. Whenever a higher Z index is found, the maximum Z index is updated. Notice that zIndex is a property of the DHTML element on Netscape Navigator, but is a property of the style object on Internet Explorer.

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: November 9, 1998
Revised: November 12, 1998

URL: http://www.webreference.com/js/column29/highestz.html