Brian Lin
asked on
Script is null or not an object
Hi, experts.
I try the following script but browser shows -- document.getElementsByTagN ame("body" )[0].style .fontSize = font_size + "px"; is null or not an object -- How can I fix it ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Q_21154225</title>
<script type="text/javascript">
myPix = new Array("dots/digital1.gif", "dots/digi tal2.gif", "dots/digi tal3.gif", "dots/digi tal4.gif", "dots/digi tal5.gif", "dots/digi tal6.gif", "dots/digi tal7.gif")
thisPic = 3
imgCt = myPix.length -1
var font_size = 14;
var MAX = 17;
var MIN = 11;
function processPrevious(num)
{if (document.images && thisPic > 0) {
thisPic--
document.myPicture.src=myP ix[thisPic ]
}
}
function processNext(num)
{
if (document.images && thisPic < imgCt) {
thisPic++
document.myPicture.src=myP ix[thisPic ]
}
}
function adjust_text(num)
{
font_size += num;
if(font_size > MAX)
{
font_size = MAX;
}
if(font_size < MIN)
{
font_size = MIN;
}
document.getElementsByTagN ame("body" )[0].style .fontSize = font_size + "px"; <--- Error
document.getElementById("t est").styl e.fontSize = (font_size - 3) + "px";
}
adjust_text(0);
</script>
</head>
<body onload="adjust_text(0);">
<table width="329" border="0">
<tr>
<td width="117" align="right"><div class="style2">Change font size</div></td>
<td width="48" align="right"><A HREF="javascript:processPr evious(adj ust_text(- 1))"><IMG SRC="arrows/reverse.gif" width="40" height="20" BORDER=0></a></td>
<td width="98" align="center"><IMG SRC="dots/digital4.gif" NAME="myPicture" width="81" height="20"></td>
<td width="48" align="left"><A HREF="javascript:processNe xt(adjust_ text(1))"> <IMG SRC="arrows/forward.gif" width="40" height="20" BORDER=0></a></td>
</tr>
</table>
<p>This is the body text</p>
<p id="test">This is the second body text</p>
<a href="font_change2.html"> 2nd page </a>
</body>
</html>
I try the following script but browser shows -- document.getElementsByTagN
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Q_21154225</title>
<script type="text/javascript">
myPix = new Array("dots/digital1.gif",
thisPic = 3
imgCt = myPix.length -1
var font_size = 14;
var MAX = 17;
var MIN = 11;
function processPrevious(num)
{if (document.images && thisPic > 0) {
thisPic--
document.myPicture.src=myP
}
}
function processNext(num)
{
if (document.images && thisPic < imgCt) {
thisPic++
document.myPicture.src=myP
}
}
function adjust_text(num)
{
font_size += num;
if(font_size > MAX)
{
font_size = MAX;
}
if(font_size < MIN)
{
font_size = MIN;
}
document.getElementsByTagN
document.getElementById("t
}
adjust_text(0);
</script>
</head>
<body onload="adjust_text(0);">
<table width="329" border="0">
<tr>
<td width="117" align="right"><div class="style2">Change font size</div></td>
<td width="48" align="right"><A HREF="javascript:processPr
<td width="98" align="center"><IMG SRC="dots/digital4.gif" NAME="myPicture" width="81" height="20"></td>
<td width="48" align="left"><A HREF="javascript:processNe
</tr>
</table>
<p>This is the body text</p>
<p id="test">This is the second body text</p>
<a href="font_change2.html"> 2nd page </a>
</body>
</html>
ASKER
yes, i did....
document.getElementsByTagN ame("body" )[0].style .fontSize = font_size + "px"; <--- Error
document.getElementById("t est").styl e.fontSize = (font_size - 3) + "px";
}
adjust_text(0); <-- is this right ?
document.getElementsByTagN
document.getElementById("t
}
adjust_text(0); <-- is this right ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks so much !!
Your welcome and thanks for the grade
Cheers
Scott
Cheers
Scott
right at the end of your script section you call
adjust_text(0);
This is trying to run before the page loads hence before body exists
If you remove this line it will be called by the onload you already have on your body tag
HTH
Scott