?
Solved

How to close all javascript opened windows

Posted on 2001-08-01
15
Medium Priority
?
186 Views
Last Modified: 2006-11-17
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
Comment
Question by:hveld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +2
15 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6342118
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
 
LVL 2

Expert Comment

by:coreyit
ID: 6342235
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6342347
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!

 
LVL 15

Expert Comment

by:a.marsh
ID: 6342372
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
 

Author Comment

by:hveld
ID: 6342420
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
 

Author Comment

by:hveld
ID: 6342455
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6342535
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
 
LVL 14

Expert Comment

by:avner
ID: 6342700
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6343512
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6343585
avner, did you actually read my earlier solution??

:oP

Ant
0
 

Author Comment

by:hveld
ID: 6344590
mplungjan, how can I 'replace window.open '
0
 
LVL 14

Expert Comment

by:avner
ID: 6344669
a.marsh , No .
But I just did ..:)
sorry.

-ac
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 400 total points
ID: 6345255
<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
 

Author Comment

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

Author Comment

by:hveld
ID: 6345428
Michel, this is really cool, thanx!
btw looks like just
window.open=null;
is enough
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question