?
Solved

window.opener.focus()

Posted on 2003-03-04
8
Medium Priority
?
400 Views
Last Modified: 2006-11-17
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
Comment
Question by:sure_krishna
[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
8 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 200 total points
ID: 8064056
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
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8064102
this may help u:

opener.location.href = opener.location.href;
0
 
LVL 1

Expert Comment

by:MatrixDud
ID: 8067585
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.

 
LVL 5

Expert Comment

by:elhy
ID: 8070084
onUnload will be activated if you refresh the page. So I don't think its a good idea afterall.
0
 
LVL 1

Expert Comment

by:MatrixDud
ID: 8071861
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
 
LVL 5

Expert Comment

by:elhy
ID: 8077207
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
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10097455
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

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

771 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