Click to See Complete Forum and Search --> : Array+window.open Problems


Paul Jr
12-11-2003, 11:48 PM
I'm attempting to create an array with a list of links in it, then open them all up as popups using a for loop.

I have this so far:

<script type="text/javascript">
var windows = new Array(10)
windows[0] = "www.teenchat.com"
windows[1] = "www.google.com"
windows[2] = "www.yahoo.com"
windows[3] = "www.php.net"
windows[4] = "www.microsoft.com"
windows[5] = "www.w3schools.com"
windows[6] = "www.w3.org"
windows[7] = "www.westciv.com"
windows[8] = "www.htmlgoodies.com"
windows[9] = "www.aspfreeserver.com"
for (i=0; i<10; i++) {
window.open(windows[i]);
}
</script>


The problem is, the windows don't open correctly.
Let's say the page containing this code is www.aspfreeserver.com/pauljr/popup.html. What's going to happen is the windows are going to popup with the url of www.aspfreeserver.com/pauljr/popup.html/urlofpopupwindow.

I don't want that, I just want them to open like a normal popup, without the www.aspfreeserver.com/pauljr/popup.html


Eh, I'm incredibly new to JavaScript, so... yeah. :D
Thanks for your help.

fredmv
12-12-2003, 12:34 AM
You need to include "http://" before the URI. Therefore, you could do:window.open('http://' + windows[i]);

Paul Jr
12-12-2003, 12:38 AM
Aight, thanks, I'll try it out.

fredmv
12-12-2003, 12:46 AM
You're welcome. I have a few suggestions regarding optimizing your code if you're interested as well.

Paul Jr
12-12-2003, 06:42 PM
It worked like a charm, excellent, thank you!

I would be very interested, please, let the suggestions flow! :D

fredmv
12-12-2003, 06:59 PM
You're welcome. I would suggest writing the script like this:<script type="text/javascript">
//<![CDATA[
var link =
[
'teenchat.com',
'google.com',
'yahoo.com',
'php.net',
'microsoft.com',
'w3schools.com',
'w3.org',
'westciv.com',
'htmlgoodies.com',
'aspfreeserver.com'
];
for(i=0; i<link.length; i++) window.open('http://' + link[i], '', '');
//]]>
</script>This way there are two advantages as opposed to how you originally wrote it: It doesn't use the Array constructor and therefore requires less resources and memory. Using this method for creating arrays is the absolute shortest way of doing it and in many cases, the best way. Each time you add another URI to the array you don't have to update the loop. This is a much more dynamic way of doing it since it keeps comparing the variable i (the counter) to the length of the array. Notice there are no curleybraces before and after the code in which needs to be executed for each iteration of the loop. Note that they aren't required when you only have one statement you want to execute.

Paul Jr
12-12-2003, 08:51 PM
Question: What are the last two empty set of quotes for?


('http://' + link[i], '', '')

fredmv
12-12-2003, 08:54 PM
Those are the other arguments the open method accepts. It goes like this:window.open([String URI], [String NAME], [String OPTIONS]);You don't need to define them (they're all optional) if you aren't going to have anything for them but I did anyway. I just figured I'd add the other arguments in case you didn't know it could accept more than one argument.

Paul Jr
12-12-2003, 08:58 PM
Alright, cool. Thanks a lot for your help! :)

fredmv
12-12-2003, 08:58 PM
You're welcome. :D