浏览器标签之间的通信 [英] communication between browser tab

查看:120
本文介绍了浏览器标签之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html页面(main.html),在window.open(newtab.html)方法中使用javascript在同一个域中打开一个新选项卡。

i've an html page (main.html) opening a new tab in the same domain using javascript with window.open("newtab.html") method.

在新选项卡中,用户可以做一些结束点击按钮的活动。
在这一点上,我想发送一条消息给opener窗口。
我尝试了postMessage,但是从新标签中我不能引用开罐器。

In the new tab users do something ending his activity clicking a button. At this point I would like to send a message to the opener window. I tried with postMessage but from new tab I can't have a reference to the opener.

从新标签中我想要类似的东西, veko

From new tab I'd like something like but I've "ko"

var w = window.opener;
if (w) {
    w.postMessage("hi", "http://10.150.10.43");
} else {
    alert("ko");
}

从次选项卡/窗口发送消息到主要的一个(在同一个域名中)?

What is the best way to send message from the secondary tab/window to the main one (in the same domain)?

非常感谢您
Luca

thank you very much Luca

推荐答案

那很奇怪。使用 window.open(newtab.html)打开一个新窗口应该使新打开的窗口能够通过 window.opener引用开启者

That is strange. Opening a new window with window.open("newtab.html") should give the newly opened window the ability to reference the opener via window.opener.

无论如何,在同一个域中的两个窗口之间还有其他几种选择。我认为以下两个最容易实现:使用 localStorage
$ b

Anyway, there are several other options for communicating between two windows on the same domain. I think that the following two are the easiest to implement:


  1. 如果您将某些数据下的某些数据存储到新选项卡窗口的localStorage中,那么开启器窗口将获得一个存储事件。如果您通过处理程序捕获事件,则可以从您从新选项卡窗口写入的localStorage中读取数据。请注意,只有当两个页面位于同一个域上,并且实际上存在两个窗口(在写入数据的同一窗口中未触发事件)时,才会触发事件。有关如何执行此操作的详情,请参阅: http://diveintohtml5.info/storage.html

使用共享网络工作者。一个web worker是一个JS脚本,在后台执行一个单独的线程。共享网络工作者是一种特殊类型的网络工作者,允许任何数量的父文档与单个工作者进行通信。因此,您可以使用工作人员在开启器窗口和新选项卡窗口之间中继消息。用于与工作人员通信的API与postMessage API非常相似。有关如何执行此操作的更多信息,请参阅:例如: http:// www.sitepoint.com/javascript-shared-web-workers-html5/

Use shared Web workers. A web worker is a JS script that is executed in the background on a separate thread. Shared web workers are a special type of Web workers that allow any number of parent documents to communicate with a single worker. So, you could use the worker to relay messages between the opener window and the new tab window. The API for communicating with a workers is very similar to the postMessage API. For more info on how to do this -- see for example: http://www.sitepoint.com/javascript-shared-web-workers-html5/.

这篇关于浏览器标签之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆