问题
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?
回答1:
Check this one http://bytes.com/topic/javascript/insights/913606-javascript-communication-between-browser-windows-tabs its all explained there.
Or see this demo http://theprivateland.com/bncconnector/demo.htm
回答2:
I found a way, I can make two flash movies on each page with LocalConnection to invoke JavaScript on the other page using externalinterface.
Put this in a AS3 swf, this is the receiver:
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:
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:
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)
回答3:
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.
回答4:
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.
回答5:
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 Window
object, then you could use the new postMessage API introduced with HTML5. I elaborated a bit on both approaches in this stackoverflow answer.
来源:https://stackoverflow.com/questions/4987582/communication-between-windows-tabs-with-javascript