javascript - 如何使用 jQuery/Javascript 将事件从一个选项卡/窗口发送到另一个选项卡/窗口

标签 javascript jquery events soundcloud

当另一个 soundcloud 窗口或选项卡正在播放时,soundcloud 如何停止当前播放的声音?

我正在寻找一种将事件从选项卡/窗口发送到另一个选项卡/窗口的方法,而不使用子窗口设计。

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

最佳答案

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)

关于javascript - 如何使用 jQuery/Javascript 将事件从一个选项卡/窗口发送到另一个选项卡/窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933682/

相关文章:

javascript - 删除重复项后重建 JSON

jquery - 就地编辑 html 表格单元格 - 输入框大小

java - 从监听器/事件更改 JComponent

javascript - 检测文本是否溢出

javascript - 提前输入 : Adding a last option

javascript - Protractor :如何确认调用了 fs.watch

javascript - 单独定位每个 DIV

php - 为什么这个 javascript 不能按照我们在弹出窗口中的目的运行

c# - 捕获 .NET TextBox 的 MouseDown 事件

ios - 为什么只为许多单独的触摸创建一个 UIEvent?