• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3277
  • Last Modified:

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?
0
BNBJ
Asked:
BNBJ
  • 6
  • 6
  • 4
  • +1
1 Solution
 
a.marshCommented:
Use:

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

:o)

Ant
0
 
BNBJAuthor Commented:
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?
0
 
a.marshCommented:
Strange.........could you paste all of the code you used?

Ant
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
a.marshCommented:
What browser are you using?

Ant
0
 
ZhongYuCommented:

Use this instead:

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

:o\

Ant
0
 
BNBJAuthor Commented:
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
0
 
pagemastahCommented:
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>

0
 
ZhongYuCommented:
Hi BNBJ,

Have you tried my code. It should work.  
0
 
a.marshCommented:
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
0
 
a.marshCommented:
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
0
 
ZhongYuCommented:
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.
0
 
pagemastahCommented:
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>

0
 
BNBJAuthor Commented:
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 ;-)

0
 
BNBJAuthor Commented:
Thanks a lot...
0
 
pagemastahCommented:
does it work?
0
 
BNBJAuthor Commented:
yep!
0
 
pagemastahCommented:
welcome! anytime ; )
0
 
ZhongYuCommented:
It works with me.
0
 
BNBJAuthor Commented:
The question is closed....thanks everyone.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now