最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cross-domain, cross-tab communication between co-operating pages - Stack Overflow

programmeradmin3浏览0评论

Say I have two tabs, each with a web-page loaded on a different domain. The pages in the two tabs want to municate.

The simplest solution I could see was this one (my answer on a closely-related question I found while searching for duplicates), where one or both of the pages load an intermediate page iFrame, which proxies between postMessage() and localStorage events. However, this does require this page to be hosted somewhere, and an extra request by the client.

Are there any techniques for this that wouldn't require a specialised "proxy page" to be served by one of the domains? (I.e. that could be implemented by a JavaScript library without a supporting server?)

Say I have two tabs, each with a web-page loaded on a different domain. The pages in the two tabs want to municate.

The simplest solution I could see was this one (my answer on a closely-related question I found while searching for duplicates), where one or both of the pages load an intermediate page iFrame, which proxies between postMessage() and localStorage events. However, this does require this page to be hosted somewhere, and an extra request by the client.

Are there any techniques for this that wouldn't require a specialised "proxy page" to be served by one of the domains? (I.e. that could be implemented by a JavaScript library without a supporting server?)

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Dec 1, 2014 at 18:10 cloudfeetcloudfeet 12.9k1 gold badge60 silver badges62 bronze badges 12
  • 1 Possible duplicate of Communication between tabs or windows – Michał Perłakowski Commented Dec 13, 2016 at 0:21
  • 4 Not a duplicate. Here the question is about cross-domain. – Mitar Commented Dec 13, 2016 at 8:35
  • why you need two domains ? without a supporting server? ( Impossible ) – btzr Commented Aug 2, 2017 at 15:12
  • 1 @btzr - Definitely not impossible! I actually link to a solution in the question, and it doesn't need any server-side ponents at all (all sites involved can be 100% static, just using client-side JS). However, the solution I linked requires an extra HTML page (loaded in iFrame) to act as a proxy, and I was wondering if there's a more elegant solution. – cloudfeet Commented Aug 2, 2017 at 18:57
  • 1 @cloudfeet maybe you can find something useful at github./wingify/across-tabs - I'm not sure how they implemented it, but it seems like a pretty in-depth implementation of this challenge. – motig88 Commented Jan 8, 2018 at 14:37
 |  Show 7 more ments

3 Answers 3

Reset to default 0

This javascript library appears to provide the functionality you're looking for (i.e., supports cross-origin munication between browser tabs). I have not used this yet, but will be trying this out in my application. Check out https://github./wingify/across-tabs.

I'd probably chose to create a backend API service as a mon munication tunnel between the 2 different websites.

Eg. Site-A send a POST message to https://your-API-service When Site-B asks for an update to https://your-API-service Then API service returns the message previously sent from Site-A If you need real-time munication you can also use WebSockets or push notifications

The window.PostMessage API is what you're looking for.

https://developer.mozilla/en-US/docs/Web/API/Window/postMessage

The window.postMessage() method safely enables cross-origin munication 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.

发布评论

评论列表(0)

  1. 暂无评论