Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

window.opener.focus()

I would be grateful if someone could give me a tip to my problem:

I have two HTML-Pages containing Java Script .The user opens a Browser window containing the first page.When the Java Script at this page is executed, it opens a new browser window containing the second page, using the window.open("HTLM-Page name") method.That means a Java Script contained in the second window can handle the first one using the window.opener object reference.
My Requirement is, if I close my first page my second page should also get closed and also if my second page is closed the focus should be set to the first page. All I have to do is to call the method window.opener.focus(). The problem is, if the user already closed this first window, I can‚t transfer the focus anymore.
My question is how can I check whether the first window was closed or if it's still opened?
I already tried to ask for the property window.opener.closed this is not a problem in IE 6 but raises an error in IE 5.0.

Thank you very much in advance.
0
sure_krishna
Asked:
sure_krishna
1 Solution
 
knightEknightCommented:
In the first page, you will have to open the window using a global variable:

<SCRIPT language='javascript'>
   var hwnd;
   // then later, use this to open the window:
   hwnd = window.open("pagename.htm");
</script>


Then do this in the body tag:

<BODY onunload='if(hwnd&&!hwnd.closed)hwnd.close();' >

And in the second page do this:

<BODY onunload='self.opener.focus();' >

Of course, the onunload event will fire even if you navigate to a new page, not just when you close the browser.
0
 
umangjoshiCommented:
this may help u:

opener.location.href = opener.location.href;
0
 
MatrixDudCommented:
If the first window is closed you want to close the second window by putting

onUnload="secondwindow.close()"

in the body tag of the first window. Also when you open the second window do this:

var secondwindow = window.open(...your open params...);

If you're just trying to tell if the window closed you can always set a variable in the second window onUnload() in the first window.

ie: <script>
       var bWindowOpen = true;
    </script>

Then when you run your script on the second page you can check that variable.

To set focus on the first window when the second window closes use this in the body tag of the second window:

onUnload="if(bWindowOpen == true) opener.focus()"
0
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.

 
elhyCommented:
onUnload will be activated if you refresh the page. So I don't think its a good idea afterall.
0
 
MatrixDudCommented:
Why not pop up the second window without toolbars so the user can't easily refresh (other than pressing F5). It's probably not a page that should ever need a user to refresh it so you could just say the heck with them and use onUnload.

The only other way I could see doing it would be to use a timer to run a function in the second window over and over again to check the status of the first window. Maybe like this:

<script>
var bWindowOpen = true; // set to false onUnload of the first window
setTimeOut( myFunc, 2000 );

function myFunc()
{
     if( bWindowOpen )
     {
          setTimeOut( myFunc, 2000 ); // recursive
     }
     else
     {
          window.close();
     }
}
</script>

put a similar script in the first window to see if the second window is closed:

<script>

function myFunc()
{
     if( secondwindow )
     {
          setTimeOut( myFunc, 2000 ); // recursive
     }
     else
     {
          self.focus();
     }
}
</script>
0
 
elhyCommented:
But nowadays a lot of ppl are computer literate, I'm sure most of the time ppl will just press F5. I'm one of them ;)
0
 
jaysolomonCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: knightEknight {http:#8064056}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now