[thelist] Re: Re: Re: Accessing layers in Javascript

Ben Gustafson Ben_Gustafson at lionbridge.com
Wed Jan 30 12:53:00 CST 2002


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
--
[ Picked text/plain from multipart/alternative ]
> > --
> > var Layer = document.getElementsByTagName("DIV");
> > for (var i = 0; i < Layer.length; i++)
> > 	if (Layer[i].getAttribute('class') == "your_class_name")
> > 		// do something
> > --
>
> Looks good, but I'm trying to loop through all layers and if the id
> contains "menu" then I want to incrementally increase a counter
> variable. This needs to work in browsers which support
> document.layers,
> document.all and document.getElementById.
>
> I've tried changing the code to
>
> if (Layer[i].getAttribute('id') == "menu1")
>
> (as a first step) but this doesn't work in NN6 or IE5/Mac. Any futher
> suggestions?

For lack of A) insight on how to fork the code for the browser without
repeating, and B) knowledge of how to get the layer ID string in Netscape
4.x, I'd try this:

<SCRIPT LANGUAGE="JavaScript1.2"><!--
var counter = 0;

if (browser == "Gecko") {
var Layer = document.getElementsByTagName("DIV");
	for (var i = 0; i < Layer.length; i++)
		if (Layer[i].getAttribute('id').indexOf("menu") != -1)
			counter++;
} else if (browser == "IE") {
var Layer = document.all.tags("DIV");
	for (var i = 0; i < Layer.length; i++)
		if (Layer[i].id.indexOf("menu") != -1)
			counter++;
} else if (browser == "NS4") {
	alert("Upgrade your piece of crap browser!");
}
//--></SCRIPT>

--Ben



More information about the thelist mailing list