15

I've seen ways of seeing if a window a particular script opened is still opened, but what if it didn't?

I have a small window that has a button to click to load the large window. When I close the large one, I want a particular onUnload or onBeforeUnload to fire iff the small one is closed; if it's still open, those procedures will not fire. I may be having simply a massive brain fart but I can't figure out how to check if the other window is open. The big one isn't opening it, so I can't simply record the handle from opening it.

In shorter terms: If window A opened window B, how can I check within window B if window A still exists?

Andrew
  • 5,095
  • 6
  • 42
  • 48

4 Answers4

25
if(window.opener && !window.opener.closed)
    alert('Yup, still there.');
chaos
  • 122,029
  • 33
  • 303
  • 309
  • Oh. Well, that was simple. Here's a followup question, though... if B was opened independently of A, is there any way for it to see if A exists? A having a particular name/target and everything, it's not like it's trying to divine what I mean by A, it knows what I mean by A. – Andrew Jan 24 '11 at 18:08
  • window.parent? That is for frames, not for popups – Ruan Mendes Jan 24 '11 at 18:10
  • Oh, jeez. Thanks, Juan. Yeah, I meant `opener`, not `parent`. Andrew: the relevant key would be the windowName argument to `window.open`, but I'm not aware of any way of using that other than by another call to `window.open`, which is pretty destructive. – chaos Jan 24 '11 at 18:13
  • 3
    Just checking window.opener doesn't tell you that it's still open, you'll get an error if you actually access a property `window.opener.name` and the window is closed. The only property you can access from a handle to a closed window is the 'closed' property – Ruan Mendes Jan 24 '11 at 18:18
  • Juan, you shame me. Good man. Answer further edited to incorporate your valued corrections. If I could upvote your answer more than once I would. – chaos Jan 24 '11 at 18:21
18

window.closed will be set to true if you popped a window and it was closed (by script or user).

var win = window.open('...')';
if (win.closed)

Your case seems to be the following:

From a popup window, you can check if the window that opened it is still open using window.opener.closed

Get handle to a window by name

I mentioned there's no way to just get the window handle by name in the comments. However, I did some research and found that the following works in FF/IE/Chrome; it's a hack, I didn't see it mentioned anywhere as the expected behavior, so I wouldn't rely on it too much, but it was fun to find it works! In my code, I would still just make sure to pass around the required handles.

//opened a window without storing a handle, but gave it a name
window.open('/some/url', 'xxx');

// now I need to get a reference to that window
// Calling open without setting a url gets you
// a reference and doesn't reload the window
var win = window.open('', 'xxx')
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Thanks. What if it can be opened a way other than through the original javascript? (i.e. if C opened B, can B see if A is open?) – Andrew Jan 24 '11 at 19:29
  • No, you cannot 'divine' the handle to a window. There is no global registry of windows by name. In your example, did C create A? if it did, you should store that handle `A = window.open(); B = window.open(); B.A = A'`. Then B could check `if (!A.closed)`; – Ruan Mendes Jan 24 '11 at 20:00
  • Thanks for the hack. I might consider using that, actually, but right now I'm trying to make it cleaner and formulate an entirely back-end solution. – Andrew Jan 25 '11 at 14:09
1

Use try{} and catch(err){}.

try{
window.opener.document.body.getElementById('#elementId'); // do some action with window. opener
}
catch(err){
// if we are here then probably there is no window.opener
window.close(); // closing this window or whatever you want
}
Audrius
  • 11
  • 1
  • Well, "probably there is no window.opener" is not an exact solution. This question is two years old and has an accepted answer. In the future, please put your effort in new questions or those without an accepted answer ;) – Mifeet Jun 01 '13 at 16:07
  • 1
    @Mifeet this answer didn't satisfy me. I just wanted to add an alternative and maybe a better solution if someone like me would come here. And there is nothing funny about "probably". Because catch may be triggered not only if window.opener doesn't exists but when your code dealing with window.opener is invalid. – Audrius Jun 03 '13 at 09:36
  • 1
    Ok, thanks for the clarification. Using a try-catch is not a recommended practice though, exceptions should be used for exceptional states, not normal execution flow. Testing if `window.opener` is set is a better solution. – Mifeet Jun 03 '13 at 11:24
1

Try the following code:

if (!!window) {
  console.log('Exist');
}
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
Ritwik
  • 1,597
  • 16
  • 17