[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Script is null or not an object

Posted on 2004-10-09
5
Medium Priority
?
178 Views
Last Modified: 2011-10-03
Hi, experts.
I try the following script but browser shows -- document.getElementsByTagName("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/digital2.gif","dots/digital3.gif","dots/digital4.gif","dots/digital5.gif","dots/digital6.gif","dots/digital7.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=myPix[thisPic]
}
}
function processNext(num)
{
if (document.images && thisPic < imgCt) {
thisPic++
document.myPicture.src=myPix[thisPic]
}
}
function adjust_text(num)
{
font_size += num;
if(font_size > MAX)
{
font_size = MAX;
}
if(font_size < MIN)
{
font_size = MIN;
}
document.getElementsByTagName("body")[0].style.fontSize = font_size + "px"; <--- Error
document.getElementById("test").style.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:processPrevious(adjust_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:processNext(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>
0
Comment
Question by:Brian Lin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:Colosseo
ID: 12267678
Hi

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
0
 

Author Comment

by:Brian Lin
ID: 12267736
yes, i did....

document.getElementsByTagName("body")[0].style.fontSize = font_size + "px"; <--- Error
document.getElementById("test").style.fontSize = (font_size - 3) + "px";
}
adjust_text(0); <-- is this right ?
0
 
LVL 15

Accepted Solution

by:
Colosseo earned 2000 total points
ID: 12268349
Hi

no with that there the code will run before body exists...

you need to remove the adjust_text(0); .... so..

document.getElementsByTagName("body")[0].style.fontSize = font_size + "px";
document.getElementById("test").style.fontSize = (font_size - 3) + "px";
}

because it will be called here...

<body onload="adjust_text(0);">

try this

 <!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/digital2.gif","dots/digital3.gif","dots/digital4.gif","dots/digital5.gif","dots/digital6.gif","dots/digital7.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=myPix[thisPic]
}
}
function processNext(num)
{
if (document.images && thisPic < imgCt) {
thisPic++
document.myPicture.src=myPix[thisPic]
}
}
function adjust_text(num)
{
font_size += num;
if(font_size > MAX)
{
font_size = MAX;
}
if(font_size < MIN)
{
font_size = MIN;
}
document.getElementsByTagName("body")[0].style.fontSize = font_size + "px";
document.getElementById("test").style.fontSize = (font_size - 3) + "px";
}
</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:processPrevious(adjust_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:processNext(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>

Scott
0
 

Author Comment

by:Brian Lin
ID: 12268846
thanks so much !!
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 12269240
Your welcome and thanks for the grade

Cheers

Scott
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question