2

The HTML5 postMesssage API allows sending message between window opener and openee. However, it requires reference/linkage to the receiver window.

Is there a pure client side JavaScript way to broadcast events to all window subscriber, under same doaminname, opened by user (e.g. Duplicate Tab) same session, without help of server?

For now, I come up with few ideas

  • Server side channel (by $_SESSION) and polling
  • JS Cooking update and polling (hard cookie play)
  • localstorage update and polling (even worst)
  • WebRTC? (poor browser support)

Is there a API I missed?

BTW, No cross-domain is need. I am thinking of same domain, multiple open by user.

Dennis C
  • 24,511
  • 12
  • 71
  • 99
  • Just use `window.postMessage("whatever", "whatever");`. If you use `window.`, it broadcasts...if you use another window reference, it only targets that one. I'm pretty sure. Did you bother testing? And here's something that might be easier to understand than the actual spec: https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage – Ian Jun 28 '13 at 03:27
  • Actually, I lied. I'm not sure why I assumed that. I guess I never truly tested it, but yeah it looks like you can't broadcast. I'm not sure if it helps, but you can look at: http://stackoverflow.com/questions/1100336/sending-a-message-to-all-open-windows-tabs-using-javascript - the second answer suggests a pretty cool library that takes advantage of `Storage` events – Ian Jun 28 '13 at 03:49

1 Answers1

1

For whom is looking for workaround solution.

I end up using window.localStorage (not even sessionStorage) and use the onstorage trigger my event handler.

There is some problem about session isolation on some browser(well, I mean IE), that the change on sessionStorage may or may not dispatch to another window. Meanwhile localStorage does better.

Dennis C
  • 24,511
  • 12
  • 71
  • 99