PDA

View Full Version : Retrieve numbers of layers


giuseppe74
07-19-2002, 11:44 AM
Hello,
i have a page with some layers, i would like to know in javascript how many layers there are on the page, it is possible?
Because then i will create a function based on the number of the layers...
Thanks,

Giuseppe

Mrs G
07-19-2002, 06:55 PM
In Netscape Navigator, the number of layers is document.layers.length.


IE

var divArray = document.all.tags("DIV");

Then use

divArray.length

This is if you are going to use the DIV tag for the layer otherwise
span

jkd
07-19-2002, 10:21 PM
And to do it properly:

document.getElementByTagName('div').length

:)

Mrs G
07-20-2002, 03:43 PM
you're right

giuseppe74
07-22-2002, 08:42 AM
i will try it later but thanks for all!!!! :)

giuseppe74
07-25-2002, 02:15 PM
yes, it works but...i should retrieve only the div tags that contain the id Layer, for example only div like this:

<div id="Layer1" style="position:absolute; left:0px; top:37px; width:150px; z-index:3; visibility: hidden">

in the page i have also a div layer that contain a flash movie that i DON'T WANT to count...how can i count only the div layers i want to use?
Thanks,

Giuseppe

giuseppe74
07-25-2002, 03:17 PM
hum...it works, there is no problem (at this moment) if i have also other div tags, but now the problem is using this in netscape...the command:

document.layers.length

does not work (in Netscape 6)...

giuseppe74
07-25-2002, 04:17 PM
...i've used this for netscape 6:

var divArray = document.getElementsByTagName('div');
numlayers = divArray.length;

but now it doesn't work with netscape 4.7.......arghhhh!!!!!

giuseppe74
07-25-2002, 04:25 PM
...i've 4 layers, and using in Netscape 4.7 the command

alert(document.layers.length) ;

it returns the value 1! :(

jkd
07-25-2002, 07:28 PM
Originally posted by giuseppe74
...i've 4 layers, and using in Netscape 4.7 the command

alert(document.layers.length) ;

it returns the value 1! :(

document.layers only refers to toplevel layer objects - nested layers aren't counted.

giuseppe74
07-26-2002, 08:41 AM
ok, there is a mode to count the number of layers in Netscape 4.7?

brothercake
07-26-2002, 10:09 AM
Yes, indirectly - in ns4 each layer is a document in itself. So you could set up a loop to look through the document of each layer object in the layers array (1 in your case) and return that length of that layers array, and then so on recursively until you;ve built up a multi-dimensional array of layer objects, or until you;ve gone as deep as you need to.

giuseppe74
07-26-2002, 10:16 AM
i've to receive a variable from a flash movie, then with this variable i call a function that open a macromedia function for hiding/showing layers.
I don't call directly the macromedia function from flash 'cause it had some problems...so i have made a myself function that do the call to the MM function.
I call the function sending one layer to show and other to be hidden, so i've created a loop that one time send a show command and other times an hide command.
The loop start from zero to...number of total layers, so i have to know the number of layers before starting the loop.
This is why i need to know the number of layers...