admin管理员组

文章数量:1290941

Is there a chance to use window.postmessage() to municate between two different applications in different tabs in the same browser?

I know you can do it between application and iFrame, but how about different tabs?

Update:

Case scenario:

  1. user plays audio from vk in one tab

  2. user starts playing video from youtube in another tab

  3. youtube sends postmessage() to vk that video started playing

  4. vk makes audio silent

Thanks

Is there a chance to use window.postmessage() to municate between two different applications in different tabs in the same browser?

I know you can do it between application and iFrame, but how about different tabs?

Update:

Case scenario:

  1. user plays audio from vk. in one tab

  2. user starts playing video from youtube. in another tab

  3. youtube. sends postmessage() to vk. that video started playing

  4. vk. makes audio silent

Thanks

Share Improve this question edited Aug 12, 2014 at 21:48 inside asked Aug 12, 2014 at 18:10 insideinside 3,17712 gold badges51 silver badges76 bronze badges 2
  • Have you seen stackoverflow./questions/3203530/… ? – Casey Falk Commented Aug 12, 2014 at 18:16
  • thanks, that was 4 years ago, so I was hoping for an update. – inside Commented Aug 12, 2014 at 18:20
Add a ment  | 

2 Answers 2

Reset to default 10

It can be done if you use an "intermediate page" loaded in an iFrame.

The (theoretical) solution uses two separate methods of inter-page munication:

  • window.postMessage()
  • localStorage or sessionStorage - see this guide for how this works; the technique involves setting values in one iFrame, and listening for events in the other iFrame.

The "intermediate page" acts as a proxy, translating message events into localStorage events, and vice-versa. If you load this "intermediate page" in an iFrame from both pages, then any messages you post in one tab will pop out in the other tab:

[Tab 1] --(postMessage)--> [iFrame 1]
                                |
                          (localStorage)
                                |
                                v
                           [iFrame 2] --(postMessage)--> [Tab 2]

If one of the tabs is on the same domain as the intermediate page (illustrated here as Tab 2), this can be simplified (without affecting the other tab's setup).

[Tab 1] --(postMessage)--> [iFrame 1]
                                |
                          (localStorage)
                                |
                                v
                             [Tab 2]

following up this thread. Same situation here. Opening a different origin using window.open("originB") from origin A. As Lakhan Jain said, theoretically we can use window.postMessage() (https://developer.mozilla/en-US/docs/Web/API/Window/postMessage) for municating between windows with different origins. we would just need to have the reference of the other window.

The problem is that we are not able to listen the events that e from the origin A even if we delay the postMessage until origin B is fully loaded.

本文标签: javascriptwindowpostmessage() to communicate between applications in different tabsStack Overflow