Passing a Javascript window reference to the session

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aelred
    New Member
    • Jan 2009
    • 3

    Passing a Javascript window reference to the session

    I have a web page where a member can open up a chat window (child window) with another member.
    - From there the member can also navigate to other web pages.
    - From other pages in the site, they may also open up new chat windows with other members (just not the same one).
    - Each chat page is opened with the member name as the window name.
    - When I log off from the web page, I would like all the chat windows to automatically close.

    I have tried to create an array to store the window references from the created chats but the problem is that once I navigate to a different page, the array is not persisted (as per the Javascript memory scope).

    I have tried the solution offered in

    However, this only works in IE and only for 1 open chat window. Once I have multiple chat windows up, it fails to close any of them. It also does not work for firefox with even 1 window.

    Ideally, I would like to save each window reference back to the server as a session variable (using AJAX). I would store each reference in a session variable array. However, as a first step, I don't seem able to get the value of the window reference from within Javascript itself. When I try to examine the value (which I understand is a reference), I get '[object Window]'.

    for example:

    Code:
    var newWin = open('chat.html', 'user1', ...)
    alert(newWin); // gives me a value of '[object Window]'
    My Ajax related code is written in anticipation of this working (see below) but unfortunately, I am passing '[object Window]'

    Code:
    function trackOpen(newWin) {
        http.open('GET', 'windowmgmt.php?a=' + newWin);
        http.onreadystatechange = handleResponse;
        http.send(null);
    
    }

    Does anyone know if this reference can be examined and I can get the value of this reference? I'm not even sure if my approach is even possible or if there is a better approach, so I am grateful for any advice.

    Thanks.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    If you see [object Window] then you have a reference which you can close with the close() method.

    For subsequent windows, keep unique references, so that the first one is not overridden. Are all new windows opened from the first main parent web page, or could they be opened from the child windows?

    Comment

    • aelred
      New Member
      • Jan 2009
      • 3

      #3
      Hi acoder,

      All new chat windows are always opened from the first main parent web page. You are not able to open a chat window from another.

      The problem is that I have to save this [object Window] reference to the session and keep the array of open windows in the session and when the user logs off make this available to the javascript.

      I will give this a try assuming that the [object Window] when passed to the session can actually be passed back and used to close the window.

      Thanks for your time to read and reply.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        No, it won't quite work like that.

        When you create a new window:
        Code:
        var win = window.open(...);
        win is a reference to the window. You can now close this window:
        Code:
        win.close();
        If you move from this page, you would lose these references.

        One possibility is to avoid pop-up windows and use chat pseudo-windows within the page using DHTML. The other possibility is to update the parent window with references to the child windows from the child windows by checking that the variable is set using window.opener.w in. If not set, reset it to 'this'. I've not tested this, but it should work.

        Comment

        • aelred
          New Member
          • Jan 2009
          • 3

          #5
          Hi acoder,

          Thank you for your suggestion. I tried the second suggestion and this seemed to work very well. The only other thing I had to do was to keep sending the child reference to the parent (since everytime I navigate to another page on parent browser, the reference is lost again). I placed the window reference assignment as per the following:

          Code:
          function saveHandle()
          {
              if (!window.opener.win) {
                  window.opener.win = this; 
              }
              setTimeout("saveHandle()",1000);
          }
          To close the window, I simply needed the following function:
          Code:
          function closeIMWindows() {
              window.win.close();
          }
          However, is it possible for window.opener.w in to be an array? since I have to keep track of multiple child windows?

          I modified the previous code to:
          Code:
          function saveHandle()
          {
          	var found = false; 
          	var openWindows = window.opener.win.length;
          	
          	for(r=0;r<openWindows;r++) {
                      
                      if (this == window.opener.win[r])
                      {
                      	found = true;
                      	break;
                      } 
              }
          
          	if (found == false){
          		window.opener.win[window.opener.win.length+1] = this;
          	}	
          
          	setTimeout("saveHandle()",1000);
          	
          }
          The issue is that 'window.opener. win.length' does not seem to be recognized and I am not sure how to specify window.opener.w in as an array. Can you provide any other suggestions?

          Thanks for your help thus far.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            win is just a variable name. It could be any valid variable name.

            It needs to be an array, so when you create windows, add them to the array:
            Code:
            var win = []; // empty array
            win[0] = window.open(...);
            In your code, you need to check for the existence of window.opener.w in to avoid errors.

            Comment

            Working...