my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes how do i get window reference without re-opening? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how do i get window reference without re-opening?" Watch "how do i get window reference without re-opening?" New topic
Author

how do i get window reference without re-opening?

K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
I want my users to be able to click on a link, and if the specified window has already been opened, I want to change focus to that window, rather than re-opening the page.
Here is the code that I have:

function openAppWindow(url, target) {
newwindow = window.open(url, target, 'width=1024,height=768,resizable=no,scrollbars=yes,status=no,toolbars=no');
newwindow.focus();
}

However, I want to get the window reference without having to call window.open. The reason is that this is an application, and user could be three screens in when they go out and do something else in another popup window. Then when they want to come back, I'd like them to be able to click on the link to go back to the popup (instead of requiring them to find the appropriate window in their task bar). If I do window.open, then it will run the first action again, and they'll have to click through a few things again to get back to where they were.

So, I'm wondering, is it possible to get a handle on the window reference without calling the window.open method? If window.open can find the appropriate window using the target, I would think I'd be able to, too, but I haven't been able to find anything that says how to do it. I have seen several places that have you store the reference in a var, but I could have several popup windows open at one time, and I don't think it's very reasonable to require me to have a separate variable and method for each possible popup in my application (i.e. different functions do not share the same popup window; each function has its own window so you can do multiple things at a time).
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6


Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Originally posted by K Dombroski:
I don't think it's very reasonable to require me to have a separate variable and method for each possible popup in my application


That's the type of thing that a Map is really good for. And while JavaScript doesn't have a native Map implementation, the dynamic, associative nature of the Object, is a pretty good approximation.

For example:



Of course, everything disappears if the user refereshes the parent page...


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
Thanks, Bear! Would it be possible to store that in the session somehow so it does not get lost when the user refreshes/navigates away and then navigates back?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Afraid not. And even if you could, the handles would be meaningless to the new page (and any references to opener in the child windows would also be invalid).
K Dombroski
Ranch Hand

Joined: Dec 03, 2005
Posts: 39
Ok, thanks much for the help! At least now, if my manager says he wants it to do that, I can tell him that it can't be done. Thanks again!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Web app designs that depend upon child and parent windows staying in synch are rather fragile.
 
 
subject: how do i get window reference without re-opening?