Solved

How to close all javascript opened windows

Posted on 2001-08-01
15
175 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 35
jQuery animation faster 1 18
Problem to page 4 26
Two RegEx conditions in one 3 17
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

760 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

16 Experts available now in Live!

Get 1:1 Help Now