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

    Display HTML in Javascript

    I'm trying to display the elements in an array but doesn't seem to work:

    Code:
    var myurl=new Array("google.com", "yahoo.com"); 
    
    for (i=0;i<=myurl.length-1;i++){
       document.writeln("<a href='http://www.' +  myurl[i] +  target='_blank'>");
    }

    Not sure why it is not working??

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    <!DOCTYPE html>
    <head>

    </head>

    <body>
    <script type="text/javascript">
    var myurl=new Array("google.com", "yahoo.com");

    for (i=0;i<=myurl.length-1;i++){
    document.writeln("<a href='http://www." + myurl[i] + "' target='_blank'>"+ myurl[i] + "</a>");
    }

    </script>

    </body>
    </html>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    In JavaScript--and indeed in virtually every computer language out there--a string starts with " and ends with the next ".

    So you have ONLY A SINGLE STRING THERE!

    Code:
    "<a href='http://www.' +  myurl[i] +  target='_blank'>"
    JavaScript WILL NOT inject stuff into the middle or a string. Period. (Unlike PHP.)

    Also, since you really ought to be using "..." around HTML attributes.

    So:
    Code:
    var myurl=new Array("google.com", "yahoo.com"); 
    
    for (i=0;i<=myurl.length-1;i++){
       var url = myurl[i];
       document.writeln('<a href="http://www.' +  url + '" target="_blank">' + url + '</a>');
    }
    HOWEVER... If at all possible, you should *NOT* be using document.write. It's way way obsolescent.

    ***********

    EDIT: Ehhh...too slow again. Well, you can see we basically agree. But I would definitely swap your usage of ' and ".
    Last edited by Old Pedant; 05-21-2012 at 10:52 PM.
    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
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    Thanked 1 Time in 1 Post
    Hello,

    Here is a script that I have reconfigured with from a personal template, and was originally an OnClick event. This may not work for an external JavaScript file.

    PHP Code:

    function sb()
    {
    var 
    foo "Google Site";
    var 
    foob "Yahoo Site";
    var 
    TB document.getElementById('myDiv');
    var 
    VB document.getElementById('myDiv2');
    if (
    TB)
    {
    TB.innerHTML = (foo.link("http://www.google.com"));
    }
    if (
    VB)
    {
    VB.innerHTML = (foob.link("http://www.yahoo.com"));
    }
    }
    //end of function 
    Hope this helps. Of course, there are improvements to make the code simpler and more efficient. As you can see, the link() function was used, a way to link targets in JavaScript. Getting the element by the ID using inner HTML is a more formidable way than using document.write().
    Last edited by Taro; 05-21-2012 at 11:20 PM.
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    That code only works if you *ALREADY* have <div>s set up to hold the links.
    Better:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <div id="putLinksHere"></div>
    
    <script type="text/javascript">
    var urls = [ "google.com","yahoo.com","bing.com"];
    var here = document.getElementById("putLinksHere");
    for ( var u = 0; u < urls.length; ++u )
    {
        var link = document.createElement("a");
        link.href = "http://www." + urls[u];
        link.appendChild( document.createTextNode( urls[u] ) );
        here.appendChild(link);
    }
    </script>
    </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.


  •  

    Posting Permissions

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