Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Repetitious DOM javascript not working

    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:
    Code:
    <!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>

  • #2
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Alex, Welcome to Coding Forums!

    Use semicolons instead of commas:
    Code:
      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?
    Last edited by MikeFoster; 02-15-2007 at 08:14 PM.

  • #3
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,774
    Thanks
    19
    Thanked 155 Times in 146 Posts
    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...

    Code:
    <!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>
    Last edited by chump2877; 02-15-2007 at 11:09 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #5
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, RJ!

    Mystery solved


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •