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 8 of 8

Thread: Display Array

  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 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

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by hiyatran View Post
    Code:
          document.writeln('<tr><td>openURL[i]</td></tr>');
    The red code is being taken as part of the string and not evaluated.

    Try:
    Code:
    document.writeln('<tr><td>'+openURL[i]+'</td></tr>');
    Last edited by webdev1958; 08-25-2011 at 05:35 AM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,649
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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.

  • #5
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote 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>'+openURL[i]+'</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

  • #6
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Code:
    window.open(openURL[i]);

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,649
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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++){ // do *NOT* use -1 after the .length here!!!
          document.writeln('<tr><td><a href="http://' + openURL[i] + '">' + openURL[i] + '</a></td></tr>');
       }
       document.writeln('</table>');
    }
    </script>
    </HEAD>
    <body onload="addtext()">
    </BODY>
    </HTML>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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; 08-26-2011 at 09: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.


  •  

    Posting Permissions

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