Close child window

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #1

    Close child window

    This might be a very newbieish question but here it goes.

    I have a control that "pops out" when the user clicks a link.

    The control is a <div> element with some stuff in it that is asynchronously updated every few seconds. The user can click an image in the top left corner of the <div> to "pop-out" the control.

    The pop-out action is pretty simple: I call the window.open() method to open the control in a new window. After calling the window.open() method, the control in the parent window sends a request to the server sets some server-side variables that indicates that the control is in a "popped out state", the server then hides the <div> element and displays a hyperlink that allows the user to re-display the control again (in case something weird happens when the user closes the popped-out window).


    When the user clicks the hyperlink that allows the user to re-display the control in the main window, I want to ensure that the pop-out window (child) is closed. I want to ensure that only one of these controls are active at a time per browser.

    Normally I would save a reference to the child window in the main window when the window.open() method is called...that way I could use this reference in order to close the child window.

    But, the problem is that the page posts back to the server to set the server-side variables when the user clicks the button to open the child window. Even if I used Ajax to asynchronously post back to the server, other controls on the page cause full page requests to the server that require it in order to work (changing languages etc).

    How do I save a reference to the child window that will persist across page requests?

    If that is not possible, then how do I close the child window??

    Thanks for your help.

    -Frinny
    Last edited by Frinavale; Sep 1 '10, 01:19 PM.
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    This is fairly vague, but I do think that AJAX is the answer.

    Code:
    <?php session_start();
    if (isset($_GET['active'])) {
        $_SESSION['active'] = (boolean)$_GET['active'];
    }
    
    echo (int)(!empty($_SESSION['active']));
    This would be the script that is called to set the active state and to get it. When your <div> or child window is open, set it to active. Whenever either is closed, set it to inactive. When you try to open your <div>, check if it is active. If it is, don't open the <div>.

    As for a way for a JavaScript pointer to persist across page requests, it really can't be done. You can preserve values through cookies, but not references.



    If the method I provided is too simplistic or impractical, you could create a periodically repeated AJAX request that gives the parent and child a shared unique ID. Save that ID within both windows and save it in a session variable. Then, whenever the parent is reloaded, create a new ID and save it. When the child windows realizes that it's ID is invalid, close it.

    Code:
    <?php session_start();
    if (isset($_GET['id'])) {
        $_SESSION['id'] = $_GET['id'];
    }
    
    echo isset($_SESSION['id']) ? $_SESSION['id'] : '';

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Kovik, thanks for the reply :)

      Originally posted by kovik
      kovik: This is fairly vague, but I do think that AJAX is the answer.

      Code:
      <?php session_start();
      if (isset($_GET['active'])) {
          $_SESSION['active'] = (boolean)$_GET['active'];
      }
       
      echo (int)(!empty($_SESSION['active']));
      I'm already doing something similar to this.

      When the window is closed it posts back to the server to indicate that it is closed.

      The problem is that I cannot close the child window when the user clicks the hyperlink to display the control in the parent window.

      The reason is that the JavaScript reference to the child window is lost during page requests to the server.

      Originally posted by kovik
      kovik: If the method I provided is too simplistic or impractical, you could create a periodically repeated AJAX request that gives the parent and child a shared unique ID
      The control is already posting back periodically. The whole reason why I only want 1 control running per browser is because several of these controls posting back to the server can cause the server to start refusing requests (due to "too many connections").


      I ended up simplifying the scenario a lot.
      The control no longer exists in the parent page, instead I have a hyperlink that opens the "popped-out" version of the control. The window is given a name so that the user can click the link multiple times in any tab and it will always just load in the one window.

      It kind of sucks that no one will see the cute "pop-out/pop-in" feature that I developed... c'est la vie.

      -Frinny

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        For your application to be so dependent on this only having one instance, having it "pop" out seems impractical. Especially if your server limits you in the way you're expressing.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I was finding that my development server was refusing connections when I had several of these controls running in several tabs.

          This is an intranet web application that has some data that is useful to watch. Originally I had put the control in the main page so that the user could just sit on that page and watch the data if they wanted to...or they could pop the control out and have a separate window to watch the data (in which case the main pages would stop displaying the data).

          When I noticed my web server (IIS 5...which is pretty limited) was refusing requests I decided to change the requirements of the control...and make sure that only one is running per browser.

          The easiest way to do this is to always display the control in a child window (with a given name) so that all tabs open the control in the same child window.

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            Maybe, instead of periodic checks, you only set the "active" session variable when the window is opened or closed. Then, only check if it's active on each attempt to load the <div>. Again, I'm not exactly sure if this is what you need, or even how reliable it is, but it removes the periodic checks.

            Comment

            Working...