Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: Close All Child Windows

  1. #1
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Close All Child Windows

    Hi,

    How do I close multi level childs windows from the base parent window. Some thing like this.
    A opens B opens C opens D opens E

    If I click on a button on A, I want to close A,B,C,D and E.

    Since I open C from B, A does not have a direct reference to C or may be I'm missing something here.

    Thanks

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Presuming that all the documents in the windows are stored on the same domain, you can call functions in a separate window as properties of their window.
    Code:
    window.oldopen = window.open;
    window.childWindows = [];
    function clearOldWindows() {
      var ret = 0;
      for(var i = 0; i < childWindows.length; ++i)
        if(!childWindows[i].document)
          ret += childWindows.splice(i, 1).length;
      return ret;
    }
    function closeAllChildWindows() {
      for(var i = 0; i < childWindows.length; ++i) {
        if(childWindows[i].closeAllChildWindows)
          childWindows[i].closeAllChildWindows();
        childWindows[i].close();
      }
      clearOldWindows();
    }
    window.open = function() {
      clearOldWindows();
      return childWindows[childWindows.push(window.oldopen.apply(window, arguments)) - 1];
    };
    Put that in a separate JS file, and include it in the heads of all the pages you'd like to include in this chain. Then, call closeAllChildWindows() on a window to close all its children.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    return childWindows[childWindows.push(window.oldopen.apply(window, arguments)) - 1];
    [/code]Put that in a separate JS file, and include it in the heads of all the pages you'd like to include in this chain. Then, call closeAllChildWindows() on a window to close all its children.
    Hi,

    I get the following error on that line
    Object doesn't support this property or method.
    Looks like window.oldopen.apply is giving error.

    Thanks

  4. #4
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Error with Apply

    I am also having the same issue/error, which karunakarn has posted.

    Any help ?

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Sorry, it seems IE doesn't like that. Try:
    Code:
    window.open = function() {
      var args = [];
      for(var i = 0; i < arguments.length; ++i)
        args.push(arguments[i]);
      clearOldWindows();
      return childWindows[childWindows.push(window.oldopen.apply(window, args)) - 1];
    };
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Still same Error

    I am trying to run this project on Internet Explore (IE).
    but I still get an error "Microsoft JScript runtime error: Object doesn't support this property or method"

    1. development on dotnet VS2.0
    2. Browser is : IE.

    Code is attached.
    Default page will open A:
    A will open B
    B will open C
    C will open D

    On Default page when clicked Close : should close all windows open(Default,A,B,C,D);

    Regards
    Labhesh S
    Attachment 717

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Perhaps something simpler:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    var kids=[];
    onunload=function(){
    for (var i_tem = 0; i_tem < kids.length; i_tem++)
    if(kids[i_tem].opener)
    kids[i_tem].close();
    }
    </script>
    
    </head>
    <body>
    A<br>
    <a href="b.htm" onclick="kids[kids.length]=window.open(this.href,'','width=400, height=400');return false;">b</a>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's not quite as flexible though.

    I'll debug mine later.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Okay.
    I am still waiting.
    Thanks !!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    That's not quite as flexible though.

    I'll debug mine later.
    I don't follow. How is your code more flexible? If you use the code in my post for all pages involved in the chain of open windows and open them all using similar syntax to that shown in the example, the children of any given window in the chain will close when any of its parents are closed.

    There is a problem in that Opera will not fire the page's onunload event when its window is closed and all browsers that I tested will fire it when the page is unloaded (changed) but, you could just break it out like so:

    Code:
    <script type="text/javascript">
    var kids=[];
    function closeAllKids(){
    for (var i_tem = 0; i_tem < kids.length; i_tem++)
    if(kids[i_tem].opener)
    kids[i_tem].close();
    }
    </script>
    and call closeAllKids() as desired. This script could also be external and linked to the head of each page. Its strong point is that it works.

    The real issue at stake here, in my opinion is, "Is there an event that fires when and only when a window is closed?" I don't think that there is, if there were, its use would be ideal for this. I may have missed something but, I do not see where your code, Twey, addresses this. Perhaps a poll could be established on the child windows to periodically check to see if the parent window (the .opener) is still open.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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