View Full Version : document.getElementById not working in NS4
poring
08-27-2002, 06:02 PM
I have the following code wich works fine in IE, but not in NS4. Is there any way to make this work in NS4? Thanks
<TABLE>
<TR><TD><font id="boxdescription"></font></TD></TR>
</TABLE>
<script>
if (document.getElementById)
document.getElementById("boxdescription").innerHTML = "hi";
else
boxdescription.innerHTML = "hi";
</script>
requestcode
08-27-2002, 06:12 PM
NS4 version browsers do not support document.getElementById nor do they support the use of innerHTML. You will have to use document.write("some text") to accomplish that. Here is a simplel example:
<html>
<head>
<title>Test</title>
</head>
<body>
<TABLE>
<TR>
<TD>
<font id="boxdescription" COLOR="red">
<SCRIPT LANGUAGE="JavaScript">
if(document.layers)
{
document.write("Hi!")
document.close()
}
</SCRIPT>
</font>
</TD></TR>
</TABLE>
<script>
if (document.getElementById)
{document.getElementById("boxdescription").innerHTML = "hi";}
else
{boxdescription.innerHTML = "hi";}
</script>
</body>
</html>
brothercake
08-27-2002, 07:49 PM
That's the basic theory ... but its more complex to implement in practise :rolleyes: Have a look at this example, and note a few things about it:
1 - the layer inside the ilayer - the ilayer specifies an inline element, but writing directly to ilayers is unreliable; it's more robust to put a layer inside it and write to that
2 - in ns4, each layer has its own document, so two nested layers are two documents inside each other
3 - you have to open() write() and close() to make it work
4 - it's also problematic to write to empty layers. You should have something inside it to begin with.
5 - layers are not flexible to written content - you must declare an initial width that;s enough for what you want to write into it (although you can modify it afterwards instead)
<table border=1>
<tr><td><span id="boxdescription"><ilayer id="boxdescription"><layer width=50>x</layer></ilayer></span></td></tr>
</table>
<form><input type="button" value="writeToLayer()" onclick="writeToLayer()"></form>
<script language="javascript1.2">
function writeToLayer() {
if (typeof document.getElementById!="undefined") {
document.getElementById("boxdescription").innerHTML = "hi";
}
else if (typeof document.layers!="undefined") {
var layerObj = document.layers["boxdescription"].document.layers[0];
layerObj.document.open();
layerObj.document.write("hi");
layerObj.document.close();
}
}
</script>
naveeny
08-29-2002, 01:18 AM
I just read into this and the is the new DOM standard. The only browsers that support the current DOM standard which applies to Netscape and Internet Explorer. Is NS 6 and IE 5 and higher.
So this won't work in either NS 4 or IE 4
brothercake
08-29-2002, 11:41 AM
The code I posted is the NS4 equivalent. The same thing can be acheived in IE4 simply by using the document.all collection.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.