Web Analytics Made Easy -
StatCounter Display Array - CodingForum

Announcement

Collapse
No announcement yet.

Display Array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Display Array

    I would like to display the elements in my array but it is NOT working. Here's my code:

    Code:
    <HTML>
    <HEAD>
    <TITLE>Test Input</TITLE>
    <script type="text/javascript">
    
    function addtext() {
       var openURL=new Array("http://google.com","http://yahoo.com","http://www.msn.com","http://www.bing.com");
       document.writeln('<table>');
    
       for (i=0;i<=openURL.length-1;i++){
          document.writeln('<tr><td>openURL[i]</td></tr>');
       }
       document.writeln('</table>');
    }
    </script>
    </HEAD>
    <body onload="addtext()">
    </BODY>
    </HTML>
    Here's the ouput:
    Code:
    openURL[i] 
    openURL[i] 
    openURL[i] 
    openURL[i]
    It should display:
    Code:
    http://google.com
    http://yahoo.com
    http://msn.com
    http://bing.com
    Any comments or suggestions are greatly apprecitated.
    thanks
    Daily Hot Deal

  • #2
    Originally posted by hiyatran View Post
    Code:
          document.writeln('<tr><td>[COLOR=Red]openURL[i][/COLOR]</td></tr>');
    The red code is being taken as part of the string and not evaluated.

    Try:
    Code:
    document.writeln('<tr><td>[COLOR=DarkGreen]'+openURL[i]+'[/COLOR]</td></tr>');
    Last edited by webdev1958; Aug 24, 2011, 11:35 PM.

    Comment


    • #3
      Another way to do this would be
      Code:
      <script type="text/javascript">
      function addtext() {
          var openURL = ["http://google.com","http://yahoo.com","http://www.msn.com","http://www.bing.com"];
          document.writeln( "<table><tr><td>" 
                            + openURL.join( "</td></tr><tr><td>") 
                            + "</td></tr></table>" );
      }
      </script>
      Be yourself. No one else is as qualified.

      Comment


      • #4
        and another way, but I suspect this is now getting beyond the scope of what is probably a homework or learning exercise, is to use dom methods like createElement(), appendChild() etc etc instead of document.write.

        Comment


        • #5
          Originally posted by webdev1958 View Post
          The red code is being taken as part of the string and not evaluated.

          Try:
          Code:
          document.writeln('<tr><td>[COLOR=DarkGreen]'+openURL[i]+'[/COLOR]</td></tr>');

          How would I put my array into the window.open() function

          Code:
          document.writeln('<tr><td> <a href = "" onclick="window.open(\'http://google.com\'); return false;">'+openURL[i]+'</td></tr></a>');
          So instead of window.open(\'http://google.com\');
          I tried window.open(\'+openURL[i]+\');

          but it does NOT work
          Daily Hot Deal

          Comment


          • #6
            Code:
            window.open(openURL[i]);

            Comment


            • #7
              Code:
              <HTML>
              <HEAD>
              <TITLE>Test Input</TITLE>
              <script type="text/javascript">
              
              function addtext() {
                 // simpler way to create array:
                 var openURL=["google.com","yahoo.com","www.msn.com","www.bing.com"];
              
                 document.writeln('<table>');
                 for (i=0;i<=openURL.length;i++){ [I][B]// do *NOT* use -1 after the .length here!!![/B][/I]
              [COLOR="Red"]      document.writeln('<tr><td><a href="http://' + openURL[i] + '">' + openURL[i] + '</a></td></tr>');
              [/COLOR]   }
                 document.writeln('</table>');
              }
              </script>
              </HEAD>
              <body onload="addtext()">
              </BODY>
              </HTML>
              Be yourself. No one else is as qualified.

              Comment


              • #8
                Small correction:

                for (i=0;i<=openURL.length;i++){ // delete the =

                But I do not see why the longer
                for (i=0;i<=openURL.length-1;i++) {
                should not work. You say // do *NOT* use -1 after the .length here!!! Why not?

                If you want to not display the www:-

                Code:
                for (i=0;i<openURL.length;i++){ 
                var lk = openURL[i].replace(/www./gi,"")
                document.writeln('<tr><td><a href="http://' + openURL[i] + '">' + lk +'</a></td></tr>');
                }
                And if you want to not display the domain suffix add
                Code:
                lk = lk.replace(/\..*/gi,"");
                Last edited by Philip M; Aug 26, 2011, 03:02 AM.

                All the code given in this post has been tested and is intended to address the question asked.
                Unless stated otherwise it is not just a demonstration.

                Comment

                Working...
                X
                😀
                🥰
                🤢
                😎
                😡
                👍
                👎