简体   繁体   中英

How can I use postMessage to share data between top level windows?

Using Web Messaging (postMessage), it's easy to send messages between windows including iframes and popup windows opened through Javascript. However, is it possible for two windows opened individually by the user to find each other and communicate?

As an example, if the user opens www.example.com/app.html in the browser, then the same page in another tab/windows of the same browser, I want the second window to know that it should act as a "child" of the first one and exchange a stream of events via postMessage. How do I detect the presence of another open window and how to I get a handle to it that I can use with postMessage?

i don't know if it's possible with postMessage .

but, it should be possible with localStorage or sessionStorage (which lives in the session scope).

using this approach you can write a value in one window/tab, and read it in the other window/tab, of course assuming that it's all on the same domain.

see more here: http://php-html.net/tutorials/html5-local-storage-guide/

hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM