views:

396

answers:

7

What is the best way to share data between open tabs in a browser?

As an example, Facebook panel: when you open a chat window in a tab, it's opened in other open tabs too.

+2  A: 

The only way I can think of: constant ajax communication with the server to report any user action on the other tabs.

Alsciende
+1  A: 

Given that these tabs are open with the same site in them, you might consider building an ajax script that reports user actions to server and couple it with another ajax script that reads that reports and reflects them in current window.

n1313
A: 

One way to do this is to not let the chat window be dependent on the tabs. Load the tabs as seperate AJAX components that when reloads doesn't affect the chat component.

Randell
+1  A: 

You could use AJAX (as everyone else is suggesting) or cookies if the data is small. See http://www.quirksmode.org/js/cookies.html for fun with cookies.

Justin Johnson
A: 

Depending on the requirements you can also use cookies/sessions. However, this means the data will only be accessible on the first page load of each tab.

If you already have two tabs open, changing something in one will not change the other unless you use some AJAX.

DisgruntledGoat
Not true, the cookie can be polled just like an AJAX service.
Justin Johnson
A: 

How about to use a cookie to store data in one tab and poll it in another tab? i dont know yet if a cookie is shared between tabs but just an idea now ...

Chris
+1  A: 

If the first tab opens the second tab automagically, you can do something like this:

First tab:

//open the first tab
var child_window = window.open( ...params... );

Second tab:

// get reference to first tab
var parent_window = window.opener;

Then, you can call functions and do all sorts of stuff between tabs:

// copy var from child window
var var_from_child = child_window.some_var;

// call function in child window
child_window.do_something( 'with', 'these', 'params' )

// copy var from parent window
var var_from_parent = parent_window.some_var;

// call function in child window
parent_window.do_something( 'with', 'these', 'params' )
spudly
I don't think this will work in situations where the user opens a page in a separate tab on their own.
Justin Johnson