Click to See Complete Forum and Search --> : document.body undefined in netscape
bisqui
01-20-2003, 07:50 PM
I have a feeling that this question has been asked before, but I couldn't find it through a search. My problem is that I'm trying to change the background image for the body in ns4. I keep getting a "document.body has no properties" error. I've tried just "alert(document.body)" and gives me 'undefined'. Is it even possible to access the body with javascript in NS? I can't imagine it's not, but for some reason I can't do it. Here's my test script:
<html>
<head>
<title>Untitled</title>
<script language="javascript">
function init()
{
alert(document.body);
}
</script>
</head>
<body onload="init()">
test
</body>
</html>
jdavia
01-20-2003, 10:32 PM
If the background is already an image, why not just swap it for a new one.
bisqui
01-20-2003, 10:43 PM
that's what I'm trying to do, but I keep getting a document.body has no properties error.
document.body.background, right?
jdavia
01-21-2003, 12:33 AM
<html>
<head>
<title>Untitled</title>
</head>
<body background="THE.GIF">
test
</body>
Just change the present image for your image.(gif or jpg)
Another way:
<style type="text/css">
<Body background -image;url (images\background.gif);
</style>
(The URL is the image name and where it is located)
There probably is a Script way of doing it, but I would use the first one..
gil davis
01-21-2003, 06:17 AM
Originally posted by bisqui
I'm trying to change the background image for the body in ns4.In NS 4, you cannot script anything that is not a layer, image or form. In other words, not the body. I have tried a few things, but none of them worked the way you would think they would. I have managed to change the source in the stylesheet array, but that has to be done before the body is rendered (not what you wanted).
In DOM-compliant browsers (NS 6, 7, Mozilla, IE 5, 6), the DOM way to get to the body would be
docBody = document.getElementsByTagName("BODY")[0];
gil davis
01-21-2003, 06:23 AM
If you want to change the background image before the page loads, here is an example for NS 4:
<head>
<style type="text/css">
BODY {background: url(picture1.jpg) no-repeat}
</style>
<script>
function changeIt() {
if (document.layers)
{document.tags["BODY"].backgroundImage = "url(picture2.jpg)";}
else
{document.body.style.backgroundImage = "url(picture2.jpg)";}
}
changeIt();
</script>
</head>
<body>
<table width=100% height=100%>
<tr><td>Hello, I'm Mr. Ed!</td></tr>
</table>
</body>
bisqui
01-21-2003, 11:21 AM
actually, that works perfectly for what I'm doing. Just can't load the image for IE before the body loads or else it throws a missing object error. I'm just loading it for IE via onload. Thanks for the help!