Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-21-2012, 10:28 PM   PM User | #1
hiyatran
New Coder

 
Join Date: Jun 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
hiyatran is an unknown quantity at this point
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??
__________________
Daily Hot Deal
hiyatran is offline   Reply With Quote
Old 05-21-2012, 10:47 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
<!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>
DaveyErwin is offline   Reply With Quote
Old 05-21-2012, 10:49 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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 ".
__________________
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.

Last edited by Old Pedant; 05-21-2012 at 10:52 PM..
Old Pedant is offline   Reply With Quote
Old 05-21-2012, 11:06 PM   PM User | #4
Taro
Regular Coder

 
Taro's Avatar
 
Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
Taro is an unknown quantity at this point
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().
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.

Last edited by Taro; 05-21-2012 at 11:20 PM..
Taro is offline   Reply With Quote
Old 05-21-2012, 11:51 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:50 AM.


Advertisement
Log in to turn off these ads.