...

View Full Version : Display Array



hiyatran
08-25-2011, 04:06 AM
I would like to display the elements in my array but it is NOT working. Here's my 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:

openURL[i]
openURL[i]
openURL[i]
openURL[i]

It should display:

http://google.com
http://yahoo.com
http://msn.com
http://bing.com

Any comments or suggestions are greatly apprecitated.
thanks

webdev1958
08-25-2011, 04:16 AM
document.writeln('<tr><td>openURL[i]</td></tr>');



The red code is being taken as part of the string and not evaluated.

Try:


document.writeln('<tr><td>'+openURL[i]+'</td></tr>');

Old Pedant
08-25-2011, 05:13 AM
Another way to do this would be


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

webdev1958
08-25-2011, 05:32 AM
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.

hiyatran
08-25-2011, 09:12 AM
The red code is being taken as part of the string and not evaluated.

Try:


document.writeln('<tr><td>'+openURL[i]+'</td></tr>');


How would I put my array into the window.open() function


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

webdev1958
08-25-2011, 09:23 AM
window.open(openURL[i]);

Old Pedant
08-25-2011, 07:51 PM
<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>

Philip M
08-26-2011, 07:45 AM
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:-


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

lk = lk.replace(/\..*/gi,"");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum