views:

406

answers:

4

i have 2 windows: window A and window B.
-window A and window B have same domain
-window A and window B doesn't have any parent window.

first question:
-is it possible for window A to get a reference of window B?

second question:
-what is the most elegant way to make window A notify something to window B?
(including new HTML5 specs)

2 ways i am aware of doing this:
-messaging by server: where window B regulary asks the server if window A has notified something
-messaging by local data (HTML5): when window A wants to notify something it changes the local data, window B regulary checks the local data for any change.

but the 2 ways are not so elegant.
for example it would be nice to get an reference of window B and using window.postMessage() (HTML5)

ultimate goal is to make something like facebook where if you open 4 facebook tabs, and chat in one tab, the chat is actualized on every facebook tab, which is neat!

thanks folks

+1  A: 

AFAIK, it is impossible to communicate across windows if they do not have the same parent.

If they have both been opened from a parent window, you should be able to get hold of the parent's variable references.

In the parent, open the windows like this:

childA = window.open(...);
childB = window.open(...)

in ChildA, access childB like this:

childB = window.opener.childA
Pekka
the question stills remains: what is the most elegant way to make them communicate? even indirectly, e.g. over the server.by a matter of fact that facebook made it, it is possible.thanks for the rep!
brillout.com
@romuwild: You're welcome for the rep ;-) it looks like that is the only option for you (through the server). You might be interested to know that facebook uses long polling comet, an http interaction where the server doesn't terminate the connection, instead pushing data through the connection as it becomes available. See http://en.wikipedia.org/wiki/Comet_(programming).
Andy E
or through local data instead of the server.but yeah seems to be the only option. thanks for the link, i wasn't aware of it!
brillout.com
+1  A: 

You said your:

utlimate goal is to make something like facebook where if you open 4 facebook tabs, and chat in one tab, the chat is actualize on every facebook tab, wich is neat!

That should happen as a by-product of your design, the views querying the model (probably the server) for updates to the chat, rather than your having to design in cross-view communication. Unless you're dealing with transferring huge amounts of data, why worry about it? It seems like it'll complicate things without a huge gain.

Years ago I found that if I did window.open using the name of an existing window and a blank URL, I got a reference to the existing window (this behavior is even documented on MDC and a comment on the MSDN docs suggests it works in IE as well). But that was years ago, I don't know how universal the support for it is in today's world, and of course you won't have a window name to look for unless all of your windows include a named iframe for communication, named uniquely via server-side code, and then communicated to the other windows by means of server-side code... (Scary thought: That might actually be feasible. Store the "current" window names related to a logged-in account in a table, give the list to any new window created that logs into that account, cull old inactive entries. But if the list is slightly out of date, you'll open new windows when searching for others... And I bet support is iffy from browser to browser.)

T.J. Crowder
actualy this is what i am basicly doing: the whole state of the web app is stored in the local data of the browser. since the local data is shared by window A and B, if window A changes the local data, window B would only need to re-read the local data to get the new state. but for it window B needs to know when it has to re-read the local data. One way of doing that is to make window B regulary check the local data. But it would be nicer to make window A tell window B "hey window B, check out the new state and re-read the local data!" nice rep nice trick for the window reference
brillout.com
Sounds very cool. Have fun!
T.J. Crowder
i just tried your trick combined with window.postMessage(), namely window.open(null,'windowName').postMessage('test msg',"*"). sadly didn't seem to work
brillout.com
Not `null`, an empty string; from the MDC link above: *"Providing an empty string for strUrl is a way to get a reference to an open window by its name without changing the window's location."* The comment on MSDN says the same thing. Not that I'm saying that's going to do it, but...
T.J. Crowder
it doesn't seem to make a difference.the thing is that your trick doesn't seem to work with chrome.do you know the name of the trick? so i can research an alternative for google chrome.
brillout.com
No, I'm afraid I don't know of any special name for it. Well, I did warn you I didn't know if the support across browsers was very good. :-) And I'm not amazingly surprised about Chrome, given the whole multiple processes thing...
T.J. Crowder
A: 

Hi! I have a neat way to do such trick, but with restrictions: you should allow popups for your domain and you will get one page always opened (as tab or as popup) which will implement communications between windows.

Here's an example: http://test.gwpanel.org/test/page_one.html (refresh page after enabling popups for domain)

The main feature of this trick - popup is being opened with url fragment '#' in the end, this force browser to don't change window location and store all the data. And window.postMessage do the rest.

Riki_tiki_tavi
it doesn't seems to solve my problem, but thanks anyway
brillout.com
+1  A: 

SharedWorker is the WHATWG/ HTML5 spec for a common process that can communicate amongst tabs.

rektide
nice, a shame that it doesn't seem to be implemented
brillout.com
its implemented in moz, webkit, and i believe opera.
rektide