Results 1 to 9 of 9

Thread: Display Array

  1. #1
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Display Array

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

    HTML 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. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I think you need to keep the variable outside of the quotes otherwise it is interrupted as literal.

    Code:
    document.writeln('<tr><td>' + openURL[i] + '</td></tr>');
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    I think you need to keep the variable outside of the quotes otherwise it is interrupted as literal.

    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

  4. #4
    Join Date
    Dec 2004
    Posts
    177
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default

    window.open(openURL[i]); should be enough.
    Verzeihung!

  5. #5
    Join Date
    Aug 2011
    Location
    both, Germany and Bulgaria
    Posts
    54
    Thanks
    14
    Thanked 10 Times in 10 Posts

    Lightbulb Quotes

    You have just to watch out the quotes and what shall be passed as html output. To become what you want, write your JavaScript like Minos said:

    document.writeln('<tr><td> <a href = "#" onclick="window.open(' + openURL[i] + '); return false;">'+openURL[i]+'</td></tr></a>');

    As you can see, I've used single quotes to mark the argument of the writeln method. All double qutes inside the single quotes are just output. The words and letters inside single quotes here, are called String. You can add Strings by using the "+" sign. To interpret what is happening inside the writeln:

    ' ... ' + openURL[i] + ' ... ' + openURL[i] + ' ... ' gives one whole String, where the values of your Array openURL[i] are read out and added to the content inside ' ... '.

    Cheers

  6. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    With that line, you're not ultimately including the URL within double quotes. You need to insert the double quotes, or it will still cause a JavaScript syntax error.

    Code:
    document.writeln('<tr><td> <a href = "#" onclick="window.open("' + openURL[i] + '"); return false;">'+openURL[i]+'</td></tr></a>');
    - Josh

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    he opened the string with a single quote, so the single quote will close it (allowing him to concatenate the variable). however, the double quotes in your example are inside the single-quoted string, so they will be interpreted literally (and mess up the output html). what I mean:
    HTML Code:
    <!--malformed-->
    <a href = "#" onclick="window.open("openURL[i]value"); return false;">
    
    <!--it should look like this-->
    <a href = "#" onclick="window.open('openURL[i]value'); return false;">
    so, you need to use single-quotes, but escape them:
    Code:
    document.writeln('<tr><td> <a href = "#" onclick="window.open(\'' + openURL[i] + '\'); return false;">' //etc...
    btw, locbtran, your html tags are improperly nested:
    HTML Code:
    <!--you can't do this-->
    <tr><td><a></td></tr></a>
    
    <!--you need to do this-->
    <tr><td><a></a></td></tr>
    as an interesting side note, .writeln() can use comma ( , ) instead of plus ( + ) concatenation. I don't know, but if it's anything like php, then the comma will be significantly faster.
    Last edited by traq; 08-26-2011 at 02:36 PM.

  8. The Following User Says Thank You to traq For This Useful Post:

    locbtran (08-26-2011)

  9. #8
    Join Date
    Aug 2011
    Location
    both, Germany and Bulgaria
    Posts
    54
    Thanks
    14
    Thanked 10 Times in 10 Posts

    Talking

    Oops, you are totally right, the window.open argument needs also quotes. Thanks for the note!!
    HTML Code:
    document.writeln(' <a href = "#" onclick="window.open(\' ',openURL[i],'\'); return false;">',openURL[i],'</a>')
    @traq, it will not function with double quotes, also not if you escape them. You'll need definitely escaped single quotes there, just to separate the <a> tag arguments from the window.open argument later in the HTML. Otherwise it becomes a mess like this:
    HTML Code:
    <a href="#" onclick="window.open(" http:="" url_1.com");="" return="" false;"="">
    BTW, of course are commas better than adding all Strings into one piece
    Last edited by XManBG; 08-26-2011 at 06:30 PM.

  10. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by XManBG View Post
    @traq, it will not function with double quotes, also not if you escape them. You'll need definitely escaped single quotes there...
    yeah, you're right. I knew I liked that way for a reason

Bookmarks

Posting Permissions

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