Solved

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

Posted on 2001-06-15
20
3,236 Views
Last Modified: 2007-11-27
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
Comment
Question by:BNBJ
  • 6
  • 6
  • 4
  • +1
20 Comments
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
Use:

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

:o)

Ant
0
 

Author Comment

by:BNBJ
Comment Utility
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
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
Strange.........could you paste all of the code you used?

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
What browser are you using?

Ant
0
 
LVL 2

Expert Comment

by:ZhongYu
Comment Utility

Use this instead:

if( window.opener == null || window.opener.closed ){
 alert("window is not open")
}
else{
 alert("window is open")
}
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
And that won't make any difference - all you have done is reversed the logic of the if statement........

:o\

Ant
0
 

Author Comment

by:BNBJ
Comment Utility
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
 
LVL 1

Accepted Solution

by:
pagemastah earned 100 total points
Comment Utility
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
 
LVL 2

Expert Comment

by:ZhongYu
Comment Utility
Hi BNBJ,

Have you tried my code. It should work.  
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
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
 
LVL 2

Expert Comment

by:ZhongYu
Comment Utility
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
 
LVL 1

Expert Comment

by:pagemastah
Comment Utility
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
 

Author Comment

by:BNBJ
Comment Utility
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
 

Author Comment

by:BNBJ
Comment Utility
Thanks a lot...
0
 
LVL 1

Expert Comment

by:pagemastah
Comment Utility
does it work?
0
 

Author Comment

by:BNBJ
Comment Utility
yep!
0
 
LVL 1

Expert Comment

by:pagemastah
Comment Utility
welcome! anytime ; )
0
 
LVL 2

Expert Comment

by:ZhongYu
Comment Utility
It works with me.
0
 

Author Comment

by:BNBJ
Comment Utility
The question is closed....thanks everyone.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now