• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

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
0
hveld
Asked:
hveld
  • 5
  • 5
  • 2
  • +2
1 Solution
 
a.marshCommented:
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
0
 
coreyitCommented:
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
0
 
a.marshCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
a.marshCommented:
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
0
 
hveldAuthor Commented:
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?
0
 
hveldAuthor Commented:
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.
0
 
a.marshCommented:
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
0
 
avnerCommented:
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.

0
 
Michel PlungjanIT ExpertCommented:
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
0
 
a.marshCommented:
avner, did you actually read my earlier solution??

:oP

Ant
0
 
hveldAuthor Commented:
mplungjan, how can I 'replace window.open '
0
 
avnerCommented:
a.marsh , No .
But I just did ..:)
sorry.

-ac
0
 
Michel PlungjanIT ExpertCommented:
<html>
<head>
<script>
saveopen=window.open;
window.open=null;
</script>
</head>
<body onLoad="window.open=saveopen">

as I said - this may be in violation of your hoster

Michel
0
 
hveldAuthor Commented:
Michel, this is really cool!
btw it looks like just
window.open=null;  is enough, although i only tested it with IE
thanx!
0
 
hveldAuthor Commented:
Michel, this is really cool, thanx!
btw looks like just
window.open=null;
is enough
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now