Solved

How to close all javascript opened windows

Posted on 2001-08-01
15
177 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JS Plugin Chaining 2 32
Enter key on html form page 11 48
Recordset containing single quotes 8 23
how can i remove string values 2 17
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

864 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now