I have two windows: window A and window B.
Two ways I am aware of doing this:
But the two ways are not so elegant.
For example, it would be nice to get a reference of window B and use window.postMessage() (HTML5)
The ultimate goal is to make something like Facebook where if you open four Facebook tabs and chat in one tab, the chat is up to date in every Facebook tab, which is neat!
The main features of localStorage are: Shared between all tabs and windows from the same origin. The data does not expire. It remains after the browser restart and even OS reboot.
postMessage() The window. postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.
I'm sticking to the shared local data solution mentioned in the question using localStorage
. It seems to be the best solution in terms of reliability, performance, and browser compatibility.
localStorage
is implemented in all modern browsers.
The storage
event fires when other tabs makes changes to localStorage
. This is quite handy for communication purposes.
References can be found here:
Webstorage
Webstorage - storage event
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With