views:

295

answers:

1

I hear HTML5 has window.postMessage(), but it seems to require having a handle on the window (or tab, throughout this question) you're posting the message to. What if I want to broadcast to all open windows? Is this possible?

(What I'm trying to do is warn other windows without any server round-trips when a user does something in one window that affects the others, so that they can update their content. However, while some windows may be opened from existing ones--allowing me to intercept and store references to them--some fresh windows may be opened manually by the user and then a bookmark selected or URL typed in. In this case there doesn't seem to be a way to intercept and store references.)

+3  A: 

IMO this is not possible using the postMessage. How about using sessionStorage? Writing to it should generate a storage event that should be propagated to all windows sharing the same session storage.

Rafael
Or cookies (as long as they're in the same domain)
Chetan Sastry
cookies could also be a solution, but the advantage of storage is that it generates events when performing operations on it. When using cookies you would have to check every X seconds (setInterval) for new cookies and another problem would be when to remove the cookie.
Rafael
I didn't know about session events--MDC's sessionStorage documentation is a little confusion. Thanks for the lead!
Kev
Actually, localStorage is the way to go in the end. At least in FF3.5, changing sessionStorage doesn't generate the storage event.
Kev
The thing is, as I understand the spec ( http://www.w3.org/TR/webstorage/#the-storage-event ) correctly, the event should be fired in both cases (sessionStorage and localStorage). If Firefox doesn't fire the event, IMO this is a bug.
Rafael