...

View Full Version : Repetitious DOM javascript not working



Alex Yeh
02-15-2007, 07:32 PM
Hello.

I'm new to Javascript. As an exercise, I thought I'd try to write a script that writes a phrase onto a web page repeatedly. Unfortunately, my code isn't working. Here's what I wrote:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Window-target" content="_top">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Language" content="en-us">
<meta name="Robots" content="noindex, nofollow, noarchive, noimageclick">
<meta name="MSSmartTagsPreventParsing" content="TRUE">

<title>::TESTING 1,2,3::</title>

<script type="text/javascript">
<!--
function Lurve(){
var bloogie = new Array();
var noogie = document.createTextNode('Repetition is a good thing!');
for(var i=0,i<200,i++){
bloogie[i] = noogie;
document.getElementsByTagName('p')[0].appendChild(bloogie[i]);
}
}

window.onload=Lurve;
-->
</script>

</head>

<body>

<table width="707" border="0" align="center" bgcolor="#FFFFFF">
<tr>
<td><p>&nbsp;</p></td>
</tr>
</table>
</body>
</html>
</script>

</head>

<body>

<table width="707" border="0" align="center" bgcolor="#FFFFFF">
<tr>
<td><p>&nbsp;</p></td>
</tr>
</table>
</body>
</html>

MikeFoster
02-15-2007, 08:12 PM
Hi Alex, Welcome to Coding Forums!

Use semicolons instead of commas:


for(var i=0; i<200; i++){


You're doing great. Feel free to ask any questions. For example: Why is there only one line of text generated instead of 200?

Alex Yeh
02-15-2007, 10:46 PM
Interestingly, that's precisely the question that's coming to mind at the moment ;)

So, why is there only one line of text generated, instead of 200?

chump2877
02-15-2007, 11:07 PM
You're adding the same text node, noogie, over and over again...but there is only one "noogie"...now it creates a new text node for each iteration of your loop...see below...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Window-target" content="_top">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Language" content="en-us">
<meta name="Robots" content="noindex, nofollow, noarchive, noimageclick">
<meta name="MSSmartTagsPreventParsing" content="TRUE">

<title>::TESTING 1,2,3::</title>

<script type="text/javascript">
<!--
function Lurve(){
var bloogie = new Array();
for(var i=0;i<200;i++){
bloogie[i] = document.createTextNode('Repetition is a good thing!');
document.getElementsByTagName('p')[0].appendChild(bloogie[i]);
}
}

window.onload=Lurve;
-->
</script>

</head>

<body>

<table width="707" border="0" align="center" bgcolor="#FFFFFF">
<tr>
<td><p>&nbsp;</p></td>
</tr>
</table>
</body>
</html>
</script>

</head>

<body>

<table width="707" border="0" align="center" bgcolor="#FFFFFF">
<tr>
<td><p>&nbsp;</p></td>
</tr>
</table>
</body>
</html>

Alex Yeh
02-15-2007, 11:16 PM
Thanks, RJ!

Mystery solved :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum