...

View Full Version : Closing several windows, open or not



clagnol
12-10-2004, 11:49 AM
I've used javascript in links on one page to open several small windows. At the bottom of the page is a link which I desire to close all of the small windows I've opened. Currently, I am using the code:

function close_all()
{
win1.close()
win2.close()
win3.close()
win4.close()
win5.close()
}

... which only works if all windows are open. If the user has decided to open only windows 1,2,4 and 5, the link at the bottom only closes 1 and 2, leaving 4 and 5 open. If any of you know a method of closing all windows, regardless of user actions, please assist me.

Kor
12-10-2004, 12:32 PM
try:

if(win1!=null){
win1.close()
}
if(win2!=null){
win2.close()
}
...

You may use a loop to shorten the code, I guess...

clagnol
12-11-2004, 08:52 AM
Thank you. That worked brilliantly.

glenngv
12-11-2004, 09:04 AM
function close_all()
{
var w;
for (var i=1; i<=5; i++) {
w = window["win" + i];
if (w && !w.closed) w.close();
}
}

Kor
12-12-2004, 07:15 PM
yeap Glenn, that's the loop



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum