Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-06-15
20
Medium Priority
?
3,252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 4
  • +1
20 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6194665
Use:

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

:o)

Ant
0
 

Author Comment

by:BNBJ
ID: 6194706
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
ID: 6194717
Strange.........could you paste all of the code you used?

Ant
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 15

Expert Comment

by:a.marsh
ID: 6194718
What browser are you using?

Ant
0
 
LVL 2

Expert Comment

by:ZhongYu
ID: 6194725

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
ID: 6194730
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
ID: 6194731
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 400 total points
ID: 6194734
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
ID: 6194747
Hi BNBJ,

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

Expert Comment

by:a.marsh
ID: 6194755
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6194766
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
ID: 6194767
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
ID: 6194769
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
ID: 6194774
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
ID: 6194778
Thanks a lot...
0
 
LVL 1

Expert Comment

by:pagemastah
ID: 6194783
does it work?
0
 

Author Comment

by:BNBJ
ID: 6194788
yep!
0
 
LVL 1

Expert Comment

by:pagemastah
ID: 6194793
welcome! anytime ; )
0
 
LVL 2

Expert Comment

by:ZhongYu
ID: 6194815
It works with me.
0
 

Author Comment

by:BNBJ
ID: 6194825
The question is closed....thanks everyone.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

721 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