Entry
How do I (through javascript) find out to which layer a link belongs?
Jun 12th, 2002 06:40
David Blackledge, Kenny Johansson,
There are two ways to go about this... 1) start from the document and
find the link, keeping track of what layer you're looking in. 2) start
from the link, and work upwards until you find the layer reference you
want.
Based on the wording of your question I'm going to assume you want this
for Netscape 4, so I'll provide the code for that. Also, if know the
link and not the layer, you're probably starting from the link, so let's
assume that and use method 2.
In Netscape, you always have the property "parentLayer" available if
you're within a layer's context. If you're not in a layer, parentLayer
won't exist, so your "parentLayer" is actually the window.
A href="blah" onclick="var containingLayer = window;
if(typeof(parentLayer) != "undefined") containingLayer = parentLayer;
myFunc(containingLayer);return false;"
In IE, you always have the "offsetParent" property available, but this
may not identify a "layer" or it may. It depends what you consider to
be a layer. What you could do is go through the offsetParents
recursively until you find one that has a defined "id" property.
onclick="var containingLayer = offsetParent; while(containingLayer.id ==
null && containingLayer != document.body)containingLayer =
containingLayer.offsetParent;"
If you wanted to start from the document and work your way down, you'd
have to define a recursive function that goes through all of the layer
objects in Netscape and checks each one's document.links array for your
current link. In IE you'd have to check each child element's children
recursively until you find the current link, remembering what the last
layer-like object was. I'm not going to get into those because they're
more complex and probably not what is needed here.
David.
http://David.Blackledge.com