• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can I close a previously opened window?

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i personally would set the window.open to an object, makes it look cleaner
NewWin=window.open(///////
NewWin.close();
 
Kelly Dolan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic