www.webdeveloper.com
Results 1 to 5 of 5

Thread: Close window opened by another frame

  1. #1
    Join Date
    Apr 2007
    Posts
    1,664

    Close window opened by another frame

    I have a site with 3 frame sets, top, left and right. I hate frames, you hate frames, we all hate frames but they are unfortunatly I'm stuck with them, and no I can't change them, it's not my site.

    A link in the right frame opens a window.

    HTML Code:
    cp=window.open('http://google.com','cp','width=980,height=735,toolbar=yes,status=yes,location=no,resizable=yes,scrollbars=yes',true);
    cp.focus();
    I would like to set and onbeforeunload event in the left frame that will close the window if it has been opened. The point is, if the main window is refreshed, the opened window is closed (if it has been opened).

    Just adding an event calling cp.close() is getting an undefined error which is understandable as it's a different frame. How would I pass the reference of the new window to the left frame if the right frame opened it?

    I should ad this is not a regular website but an administration utility with very limited and controlled access, so we only have to support the latest version of FF and IE.
    Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,666
    If cp is a global variable, you can simply do this:
    Code:
    top.frames['rightFrameName'].cp.close();
    If it is not a global variable -- then make it into one.

  3. #3
    Join Date
    Apr 2007
    Posts
    1,664
    Here's a mock up that is giving the same problem.

    Frameset
    HTML Code:
    <html>
    <frameset rows="20&#37;,70%">
    <frame src="frame_a.html">
    	<frameset cols="25%,75%">
    	<frame src="frame_b.html">
    	<frame src="frame_c.html">
    	</frameset>
    </frameset>
    </html>
    frame a
    HTML Code:
    <html>
     <head>
     </head>
     <body>
      frame a
     </body>
    </html>
    frame b
    HTML Code:
    <html>
     <head>
     </head>
     <body>
      frame b
      <p onclick="top.frames['frame_c'].cp.close();">close</p>
     </body>
    </html>
    frame c
    HTML Code:
    <html>
     <head>
      <title> new document </title>
    <script type="text/javascript">
    <!--
    var cp;
    function openwindow(){
    	cp=window.open('http://google.com','cp','width=980,height=735,toolbar=yes,status=yes,location=no,resizable=yes,scrollbars=yes',true);
    	cp.focus();
    	}
    //-->
    </script>
     </head>
     <body>
      frame c
      <p onclick="openwindow()">open window</p>
      <p onclick="cp.close()">close</p>
     </body>
    </html>
    This opens the window from the onclick in frame c but the close onclick in frame b throws the error
    Code:
    top.frames.frame_c.cp is null or not an object
    Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,666
    I don't see any name attribute on those frames.

  5. #5
    Join Date
    Apr 2007
    Posts
    1,664
    I'm an idiot! Too long in front of a PC today!

    Thanks, it now works as expected.

    HTML Code:
    <html>
    <frameset rows="20%,70%">
    <frame src="frame_a.html" name="frame_a">
    	<frameset cols="25%,75%">
    	<frame src="frame_b.html" name="frame_b">
    	<frame src="frame_c.html" name="frame_c">
    	</frameset>
    </frameset>
    </html>
    Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles