windows 使用 JavaScript 在窗口/选项卡之间进行通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4987582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:10:18  来源:igfitidea点击:

Communication between windows/tabs with JavaScript

javascriptwindowscross-browsercommunication

提问by James T

How can I have two tabs (or windows) that are on the same domain be able to talk to each other without having to have one window instance the other.

我怎样才能让同一个域中的两个选项卡(或窗口)能够相互交谈,而不必让另一个窗口实例。

Edit: Why was this marked as duplicate when this was asked before the other question?

编辑:为什么在其他问题之前询问此问题时将其标记为重复?

回答by James T

I found a way, I can make two flash movies on each page with LocalConnection to invoke JavaScript on the other page using externalinterface.

我找到了一种方法,我可以使用 LocalConnection 在每个页面上制作两个 Flash 电影,以使用 externalinterface 在另一个页面上调用 JavaScript。

Put this in a AS3 swf, this is the receiver:

将其放入 AS3 swf,这是接收器:

import flash.external.ExternalInterface;
import flash.net.LocalConnection;

var mLocalConnection:LocalConnection;
mLocalConnection = new LocalConnection();
mLocalConnection.connect("xivioview");
mLocalConnection.client=this;

function recieveText(textRecieved):void {
ExternalInterface.call(textRecieved);
};

And the sender swf:

和发件人 swf:

import flash.external.ExternalInterface;
import flash.net.LocalConnection;

function sendtoview(con,val):String {
//create local connection for sending text
var sending_lc:LocalConnection;
sending_lc = new LocalConnection();
sending_lc.send("xivioview", "recieveText", val);
return "kk"
}
ExternalInterface.addCallback("sendtoview", sendtoview);

This is set up for one-way, and the javascript to use it:

这是为单向设置,以及使用它的 javascript:

document.getElementById("youembeddedobject").sendtoview("xivioview","alert('Hai!')")

That will execute that JavaScript code in the receiver's tab, but it won't execute until you go back to that tab (I already asked a question why, and have no response yet)

这将在接收者的选项卡中执行该 JavaScript 代码,但在您返回该选项卡之前它不会执行(我已经问过为什么,但还没有回复)

回答by Loran Hayden

I would just have a javascript execute in the page load that would continuously poll (window.setInterval) the sessionStore for a flag saying someone sent me a message then read that message from the sessionStore and then do whatever is required.

我只会在页面加载中执行一个 javascript,它会不断轮询 (window.setInterval) sessionStore 以获得一个标志,说明有人向我发送了一条消息,然后从 sessionStore 读取该消息,然后执行任何需要的操作。

回答by Martin Andersson

Communicating between different JavaScript execution context was supported even before HTML5 if the documents was of the same origin. If not or you have no reference to the other Windowobject, then you could use the new postMessage APIintroduced with HTML5. I elaborated a bit on both approaches in this stackoverflow answer.

如果文档来源相同,甚至在 HTML5 之前就支持在不同的 JavaScript 执行上下文之间进行通信。如果没有或者您没有引用其他Window对象,那么您可以使用HTML5 引入的新postMessage API。我在这个stackoverflow answer 中详细阐述了这两种方法。

回答by Bart

The only way I can think of would be to use XHR. Each window/tab communicates with the server, which in turn communicates back with other windows, pretty much the same way gmail chat works. Except that you would have 2 windows on the same client, rather than 1 window on 2 clients.

我能想到的唯一方法是使用 XHR。每个窗口/选项卡都与服务器通信,服务器又与其他窗口通信,这与 gmail 聊天的工作方式几乎相同。除了在同一个客户端上有 2 个窗口,而不是在 2 个客户端上有 1 个窗口。