473,836 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a Javascript window reference to the session

3 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. var newWin = open('chat.html', 'user1', ...)
  2. 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]'

Expand|Select|Wrap|Line Numbers
  1. function trackOpen(newWin) {
  2.     http.open('GET', 'windowmgmt.php?a=' + newWin);
  3.     http.onreadystatechange = handleResponse;
  4.     http.send(null);
  6. }

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.

Jan 22 '09 #1
5 10312
16,027 Recognized Expert Moderator MVP
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?
Jan 23 '09 #2
3 New Member
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.
Jan 23 '09 #3
16,027 Recognized Expert Moderator MVP
No, it won't quite work like that.

When you create a new window:
Expand|Select|Wrap|Line Numbers
  1. var win = window.open(...);
win is a reference to the window. You can now close this window:
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 26 '09 #4
3 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. function saveHandle()
  2. {
  3.     if (!window.opener.win) {
  4.         window.opener.win = this; 
  5.     }
  6.     setTimeout("saveHandle()",1000);
  7. }
To close the window, I simply needed the following function:
Expand|Select|Wrap|Line Numbers
  1. function closeIMWindows() {
  2.     window.win.close();
  3. }
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:
Expand|Select|Wrap|Line Numbers
  1. function saveHandle()
  2. {
  3.     var found = false; 
  4.     var openWindows = window.opener.win.length;
  6.     for(r=0;r<openWindows;r++) {
  8.             if (this == window.opener.win[r])
  9.             {
  10.                 found = true;
  11.                 break;
  12.             } 
  13.     }
  15.     if (found == false){
  16.         window.opener.win[window.opener.win.length+1] = this;
  17.     }    
  19.     setTimeout("saveHandle()",1000);
  21. }
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.
Jan 26 '09 #5
16,027 Recognized Expert Moderator MVP
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:
Expand|Select|Wrap|Line Numbers
  1. var win = []; // empty array
  2. win[0] = window.open(...);
In your code, you need to check for the existence of window.opener.w in to avoid errors.
Jan 26 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Martin Lucas-Smith | last post by:
Having re-read www.php.net/functions.arguments recently, the notion of passing arguments by reference to a function makes a lot more sense now. However, my question is: is there any difference in outcome between: function add_some_extra(&$string) { $string .= 'and something extra.'; }
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back in my variables ?? note1: I'm not the one who wrote the web service.. so I can't modify
by: David | last post by:
Hi, I use the following code to open a javascript window. Is it possible to modify this to have the standard toolbar open in the window? Thanks <script language="JavaScript" type="text/javascript"> function popup() {
by: learner | last post by:
Hi, In a page, i have many links. I have some functions in a global file which is included in all linked pages. I want to have a reference to a window which is to be opened on clicking one link and it should be known only to one more link on that page and not to other links. I cant have that window reference in that global file, since iam declaring it initially as Null and modifying its value in that one particular link. Since all links...
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function defined in "A.html", but every attempt results in an "is not a function" error. I have tried to invoke the function using parent.document.funcname(), top.document.funcname(), and various other identifying methods, but all result in the above...
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
by: Geoff Cox | last post by:
Hello, The code below is aimed at passing the date in the yyyyMMdd format from the javascript calendar in an html file to the php in a another file which then searches a MySQL database. For some reason the sendPhp is not working. I need to send
by: zzapper | last post by:
Hi, I use a JavaScript window to access a library of songs when the user picks one this is written to the users database record (PHP/MySQL). When the use closes the JavaScript window he returns to the original browser window. Unfortunately unless he manually refreshes this window he cannot see that he has succeeded in selecting a song. Any ideas how I can force the refresh from the Javascript window, or any other work-around?
by: gvamohan | last post by:
hai every one iam opening an window through javascript <script type="text/javascript" language="javascript"> var k=false; window.open("main.aspx","","status=no,fullscreen=yes"); window.resizeTo(1,1) k=true; window.onresize = function()
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.