Link to home
Start Free TrialLog in
Avatar of hveld
hveld

asked on

How to close all javascript opened windows

Hi,

is there a way from the current document to close all windows opened from it by javascript?
if I use script like this to open the new window:

win1 = window.open('','newwin');
win1.document.write(some html code);
win1.document.close();

How to close it from the opener window? Note that I may not know the 'win1' name, I just want to close at once all javascript opened windows.
Thanks
Avatar of a.marsh
a.marsh

Unless you keep a reference I don't know of any way to close a child window, let alone multiple child windows, from the parent (opener) window.

Ant
I believe there is a way to make a child window dependent on its opener (if parent closes, child must -- Ant?). Anyway, if so, you could probably pass the window.open functionality through a hidden window and when you need to close them all, just close the hidden one.

Might be worth a try anyway.

-corey
As far as I know coreyit, that too is only possible by storing a reference to the child window(s).

hveld, is there any real reason why you cannot have a reference to each child?? It is common to store a reference in an array and then loop through the array to close them.

Ulitmately, you don't need to worry about the name of a reference, the code can take care of it for you....

Ant
Here's an example:

<html>
<head>
<script language="JavaScript">

winds = new Array();

currentWin = 0;

function openWin(url){
  winds[currentWin++] = window.open(url);
}

function closeAll() {
    for (var i = 0; i < winds.length; i++) {
        if (winds[i] != null && !winds[i].closed){
          winds[i].close();
        }
    }
}
</script>
</head>
<body onUnload="closeAll();">
<a href="javascript: void(0);"
onClick="openWin('http://www.yahoo.com/');">open Yahoo</a><br>
<a href="javascript: void(0);"
onClick="openWin('http://www.google.com/');">open Google</a><br>
<a href="javascript: void(0);"
onClick="openWin('http://www.altavista.com/');">open Altavista</a>
</body>
</html>

As you can see each <a> tag does not need to concern itself with the window reference.

:o)

Ant
Avatar of hveld

ASKER

I have a site on a server which adds additional code to html files after I upload them. This code causes a popup window, and is different for each file I upload, that's why I can't have a reference. Also looks like this popup opens after a random period of time(after the page loads)
So I need a function that will kill all javascript generated windows opened from the current html file, and I will simply run it each 100 or 200 milliseconds.
BTW, what is a hidden window? Hidden from what?
Avatar of hveld

ASKER

a.marsh, looks like you wrote your comment at the same time with me :)
Your code works in deed, but I can't have a reference to the opened window(s) which is what you use to close them.
The only other way I can think of is to set the event of each child window to close when the opener closes - but even then you have to be able to reference the popup in order to do that!!

I'm certain you won't be able to do this....

:oP

Ant
What about this :
Keep a global variable , let say :
window.windowArr=new Array();

Whenever you open the new Window , do something like this :

window.windowArr[window.windowArr.length]=window.open(bla.bla)

then in the main window , add an   onload event :
for (var i=0;i<windowArr.length;i++)
{
  windowArr[i].close()
}


Should work in both IE and NS.

Avatar of Michel Plungjan
Hveld wants a popup killer for a freehosting site. The answer is: Depends on what method they use to open the window
There is no way to kill all windows without reference or name. It may also be in violation of the usage of the site.

To stop all windows from being opened, you can replace window.open but that has to be before the scripts that uses it.

Michel
PS:Corey: Netscape has a ..,dependant,... property on the window.open
avner, did you actually read my earlier solution??

:oP

Ant
Avatar of hveld

ASKER

mplungjan, how can I 'replace window.open '
a.marsh , No .
But I just did ..:)
sorry.

-ac
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hveld

ASKER

Michel, this is really cool!
btw it looks like just
window.open=null;  is enough, although i only tested it with IE
thanx!
Avatar of hveld

ASKER

Michel, this is really cool, thanx!
btw looks like just
window.open=null;
is enough