如何将事件从 tab/window 发送到另一个 jQuery/Javascript

How to send an event from a tab/window to another with jQuery/Javascript

当另一个 soundcloud window 或 tab 正在播放时,soundcloud 如何停止当前播放的声音?

我正在寻找一种方法来将事件从一个 tab/window 发送到另一个,而不使用子 Window 设计。

我尝试使用自定义 Cookie 事件,但它没有影响其他选项卡。

不确定您是否正在寻找这个?服务器发送事件。

http://www.w3schools.com/htmL/html5_serversentevents.asp

<script>
    var source = new EventSource("demo_sse.php");
    source.onmessage = function(event) {
        document.getElementById("result").innerHTML += event.data + "<br>";
    };
</script>

对于任何对 old-school 感兴趣的人,通过 child/parent 访问 window,有这个例子。在您的 parent 页面中,启动一个 child 页面,并保留 myWindow 引用。这允许您访问 child window 的整个 DOM。然后,您可以控制 child => parent 或 parent => 取决于您想要控制的方式。

<script>
    var myWindow = window.open("", "MsgWindow", "width=200, height=100");
    // Or if you already have a webpage:
    // var myWindow = window.open("MyWebpage.html", "MsgWindow", "width=200, height=100");

    myWindow.document.write("<script>var test = 10;</s"+ "cript>");
    myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>");

    alert(myWindow.test); // displays 10, stored in the other window.

    myWindow.close("MsgWindow");
</script>

Use localStorage.

Did you know that localStorage fires an event? More specifically, it fires an event whenever an item is added, modified, or removed in another browsing context. Effectively, this means that whenever you touch localStorage in any given tab, all other tabs can learn about it by listening for the storage event on the window object, like so:

window.addEventListener('storage', function(e){
  console.log(event.key, event.newValue);
});

Whenever a tab modifies something in localStorage, an event fires in every other tab. This means we're able to communicate across browser tabs simply by setting values on localStorage.

请参阅该文章作者的 local-storage 模块,其中提供了一个 API 可能对您有用:

ls.on(key, fn(value, old, url))
ls.off(key, fn)