-
When I have a absolutely positioned <div id="div1"> nested in a relatively positioned <div>,
document.layers['div1'].visibility = 'hidden';
or
document.div1.visibility = 'hidden';
gives an error of:
document.div1 has no properties.
Does anyone know what's the problem?
Thanks.
-
Netscape considers the contents of each DIV/LAYER as a completely new document; thus, to access 'div1' you need to assign your outer div an ID (say 'wrapper') and then use document.wrapper.document.div1.
Here's an example:
Code:
<html>
<head>
<title> type_Document_Title_here </title>
</head>
<body>
<div id="wrapper" style="position:relative">
<div id="div1" style="position:absolute;top:100;left:100;width:50;height:50">
<table bgcolor="red" border=0 width=50 height=50>
<tr>
<td valign=middle align=center>
<font color="white">Hi!</font>
</td>
</tr>
</table>
</div>
</div>
<script language="JavaScript">
<!--
function blink(setVisibility) {
if (document.layers) { // NS4
document.wrapper.document.div1.visibility = (setVisibility ? "visible" : "hidden");
} else if (document.all) { // MSIE
document.all.div1.style.visibility = (setVisibility ? "visible" : "hidden");
}
setTimeout("blink("+!setVisibility+")",1000);
}
setTimeout("blink(false)",1000);
//-->
</script>
</body>
</html>
-
Hey guys,
Kevin's exactly right. In NS 4, to access layers and stuff, you can't leave out anything. This is just like accessing an image in a layer. In IE 4+ and NS 6+, you can just go:
document.images["imageName"]
But in NS 4, you'd have to go:
document.layers["layerID"].document.images["imageName"]
A lot more, huh? :D
Now, Kevin's code will work, but it won't work in NS 6. for NS 6 to access a layer, it uses the W3C reccomended way of document.getElementById (IE 5 can also use this way). Anyway, you can just change Kevin's function to this:
Code:
function blink(setVisibility) {
if(document.getElementById){ //IE 5 and NS 6
document.getElementById("div1").style.visibility = (setVisibility ? "visible" : "hidden");
} else if (document.layers) { // NS4
document.wrapper.document.div1.visibility = (setVisibility ? "visible" : "hidden");
} else if (document.all) { // MSIE
document.all.div1.style.visibility = (setVisibility ? "visible" : "hidden");
}
setTimeout("blink("+!setVisibility+")",1000);
}
aDog :cool: