Link to home
Start Free TrialLog in
Avatar of BNBJ
BNBJFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I detect if window.opener is still there?

I have a page (Page A) that opens a popup (Page B).
There is a link on Page B that needs to check whether Page A still exists.  If it does it needs to run...

window.opener.location.href = 'test.htm';

If the opener page doesn't exist then I need to run...

window.open('test.htm');

How can I detect whether the opener is still there.
I've tried...

if(!window.opener){
  window.open('test.htm')
}else{
  window.opener.location.href='test.htm'
}

...and it doesn't work.

Anyone any ideas?
Avatar of a.marsh
a.marsh

Use:

if(!window.opener.closed && window.opener != null){
  //window is open
}
else{
  // window is not open
}

:o)

Ant
Avatar of BNBJ

ASKER

Tried it....got a really wierd error message.

'The callee (server [not server application]) is not available and has disappeared; all connections are invalid.  The call did not execute'

Any ideas?
Strange.........could you paste all of the code you used?

Ant
What browser are you using?

Ant

Use this instead:

if( window.opener == null || window.opener.closed ){
 alert("window is not open")
}
else{
 alert("window is open")
}
And that won't make any difference - all you have done is reversed the logic of the if statement........

:o\

Ant
Avatar of BNBJ

ASKER

no problem....here's the code

function goAdvert(id){
      if(id==1){
          if(!window.parent.opener.closed && window.parent.opener != null){
               window.parent.opener.location.href = '../solutions/sol_display.asp?SolutionID=5'              
          }else{
               window.open('../solutions/sol_display.asp?SolutionID=5','CRM')
          }
     }
}


The page I'm using to call the function is in a frameset, hence window.parent

The code works fine if the opener is open, just not when it's closed.

I'm using IE5 but it needs to work in Netscape as well if possible.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of pagemastah
pagemastah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi BNBJ,

Have you tried my code. It should work.  
Yes it should because it is the same as my code, but with the logic reversed!!!!!!!

It is offering nothing more than what my code offers............

Ant
Well there we go you see - you are using frames. Try this instead:

function goAdvert(id){
  if(id==1){
    if(!top.opener.closed && top.opener != null){
      top.opener.location.href = '../solutions/sol_display.asp?SolutionID=5'    
    }else{
      window.open('../solutions/sol_display.asp?SolutionID=5','CRM')
    }
  }
}

I'm assuming that the "child" window has frames in it....

Ant
The reason your code does not work is if window.opener == null, window.opener.closed will fail.

In my code window.opener == null will shortcut.
BNJB.

this code will automatically do the detection for you... try it, it works!

open the pageA.html... click the link to pop the pageB.html... there's a link in the pageB.html which opens the test.html in replacement for pageA.html... if pageA is closed, it creates another window... ;)

pageA.html
------------
<html>
<head>
<script language="Javascript">
window.name='pageA';
function poppageB() {
window.open('pageB.html','',config='width=300,height=300');
}
</script>
</head>
<body>
<a href="#" onClick="poppageB();">Open Page B</a>
</body>
</html>


pageB.html
------------
<html>
<head>
<script language="Javascript">
window.name='pageB';
</script>
</head>
<body>
<a href="test.html" target="pageA">Open Test Page</a>
</body>
</html>


test.html
------------
<html>
<head>
<script language="Javascript">
window.name='test';
</script>
</head>
<body>
THE TEST PAGE
</body>
</html>

Avatar of BNBJ

ASKER

ZhongYu,

Ant is right....your page is exactly the same as his with the logig reversed.....so it doesn't work.

pagemastah,

That works a treat....you can have the points.  I didn't think about working it that way round.  Simple and effective!

Ant,

Thanks for your comments anyway.


Laters ;-)

Avatar of BNBJ

ASKER

Thanks a lot...
does it work?
Avatar of BNBJ

ASKER

yep!
welcome! anytime ; )
It works with me.
Avatar of BNBJ

ASKER

The question is closed....thanks everyone.