PDA

View Full Version : Display Array



locbtran
08-25-2011, 03:21 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

bluewalrus
08-25-2011, 03:41 AM
I think you need to keep the variable outside of the quotes otherwise it is interrupted as literal.



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

locbtran
08-25-2011, 08:17 AM
I think you need to keep the variable outside of the quotes otherwise it is interrupted as literal.



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

Minos
08-25-2011, 10:39 AM
window.open(openURL[i]); should be enough.

XManBG
08-25-2011, 08:17 PM
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

JShor
08-26-2011, 01:18 AM
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.



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

traq
08-26-2011, 02:35 AM
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:
<!--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:
document.writeln('<tr><td> <a href = "#" onclick="window.open(\'' + openURL[i] + '\'); return false;">' //etc...

btw, locbtran, your html tags are improperly nested:

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

XManBG
08-26-2011, 09:53 AM
Oops, you are totally right, the window.open argument needs also quotes. Thanks for the note!!

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:

<a href="#" onclick="window.open(" http:="" url_1.com");="" return="" false;"="">

BTW, of course are commas better than adding all Strings into one piece :)

traq
08-26-2011, 02:35 PM
@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