当另一个 soundcloud 窗口或选项卡正在播放时,soundcloud 如何停止当前播放的声音?
我正在寻找一种将事件从选项卡/窗口发送到另一个选项卡/窗口的方法,而不使用子窗口设计。
我尝试使用自定义 Cookie 事件,但它没有影响其他选项卡。
最佳答案
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 thestorage
event on thewindow
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 onlocalStorage
.
请参阅local-storage该文章的作者提供的模块提供了一个可能对您有用的 API:
ls.on(key, fn(value, old, url))
ls.off(key, fn)
关于javascript - 如何使用 jQuery/Javascript 将事件从一个选项卡/窗口发送到另一个选项卡/窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933682/