File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Can I close a previously opened window? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Can I close a previously opened window?" Watch "Can I close a previously opened window?" New topic
Author

Can I close a previously opened window?

Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 98
I need to do the following...
1. From browser window #1, open browser window #2. I do this by using window.open().
2. Perform other tasks in browser window #1 (thus changing its contents).
3. From browser window #1, close browser window #2. (The reason for this step is that something particular happened in browser window #1 which invalidates what can happen in browser window #2.)
From what I have been able to determine, I cannot do this. It appears that the only way I can close browser window #2 is if in browser window #1, I hold on to the variable returned by window.open() and use it to close the opened window (e.g., newWin = window.open(); newWin.close()). Unfortunately, I cannot do this because the contents of browser window #1 can change.
The purpose of my post is to gain confirmation of my findings and to find out if anyone has run into a similar problem and either knows how to do this or knows a different approach I can take to my problem.
Very many thanks!
- Kelly
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 411
Well,
if you opened window using
window.open("http://www.yahoo.com", "WndName")
then to close it, you need to todo:
window.open("", "WndName").close();
the side effect is: if you did not open window before, it will be opened and close.
There is also a funny behavior on IE5+, that you could use:
Properties added/change on window.location remains during window lifetime.
So, if when you open window, you do:
window.location.myWnd = window.open("http://www.yahoo.com", "WndName");
than to close it:
if(window.location.myWnd && !window.location.myWnd.closed){
window.location.myWnd.close();
window.location.myWnd = null;
}
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
i personally would set the window.open to an object, makes it look cleaner
NewWin=window.open(///////
NewWin.close();
Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 98
Thanks! The IE5+ solution is not an option since I have to support multiple browsers. I will consider the other option but I'm not quite sure its side effect is permissable.
A co-worker found another option that works but again seems to be IE-specific. It involves using the navigator object. Even though this is not an option for me, I post this for others to see.
function openWindow() {
navigator.mySecondWindow = window.open(...);
}
function closeWindow() {
navigator.mySecondWindow.close();
}
 
IntelliJ Java IDE
 
subject: Can I close a previously opened window?
 
Threads others viewed
How to close a window in GWT ?
opening page without toolbar, address bar etc..
JSP file doesnt show after window.open()
iframe and struts url won't work
Disabling browser /sub window buttons....
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture