SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Display Array

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    The code that displays your variable is actually just outputting a string.
    Code JavaScript:
     document.writeln('<tr><td>openURL[i]</td></tr>');

    To use variables and strings you'll have to concatenate them together. for example:
    Code JavaScript:
    var someVar = "this is the first part " + anotherVariable + " this is the second part";

    In this instance you would do it as follows:
    Code JavaScript:
     document.writeln('<tr><td>' + openURL[i] + '</td></tr>');
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Also, just to dig in to your code a bit further (hope you don't mind)

    You don't need to use "new Array()" to instantiate arrays, square brackets will do the job
    Code JavaScript:
    var anArray = ["item 1", "item 2", "item 3"];

    "document.write" and it's variants are usually poor for performance, if you're just testing stuff it doesn't matter too much, but it's not the best habit to get in to.

    When you're injecting content; where possible, it's better to inject 1 big chunk than several smaller ones.

    You could use innerHTML for example to add content to the page
    Let's say you have a <div id="test"></div> in your body section somewhere, you could then do something along the following lines:


    Code JavaScript:
    function addtext() {
       var openURL= ["http://google.com", "http://yahoo.com", "http://www.msn.com", "http://www.bing.com"];
       var htmlStr = '<table>';
     
       for (i=0;i<=openURL.length-1;i++){
          htmlStr += '<tr><td>' + openURL[i] + '</td></tr>';
       }
     
       htmlStr += '</table>';
       document.getElementById("test").innerHTML = htmlStr;
    }
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AussieJohn View Post
    The code that displays your variable is actually just outputting a string.
    Code JavaScript:
     document.writeln('<tr><td>openURL[i]</td></tr>');

    To use variables and strings you'll have to concatenate them together. for example:
    Code JavaScript:
    var someVar = "this is the first part " + anotherVariable + " this is the second part";

    In this instance you would do it as follows:
    Code JavaScript:
     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

  5. #5
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    When you're passing in openURL[i] you'll need to not escape the single quotes. At the moment you're effectively putting the string "openURL[i]" in window.open.

    e.g.
    Code:
    document.writeln('<tr><td> <a href = "" onclick="window.open('+openURL[i]+'); return false;">'+openURL[i]+'</td></tr></a>');
    Should do the trick
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }


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
  •