使用localStorage 的storage event 解决多tab 数据共享问题

主要学习下使用localStorage 的storage event 解决多tab 数据共享问题

参考代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="clickdemo" onclick="demoapp()">demo app</div>
 
    <div id="container">
        <ul id="items">
        </ul>
    </div>
    <script>
        window.addEventListener("storage",(e)=>{
            let liElement = document.createElement("li")
            liElement.innerText=`${e.newValue}====${e.oldValue}=====${e.key}`
            document.querySelector("#items").appendChild(liElement)
        })
 
        function demoapp(){
            localStorage.setItem("name",`demoapp-${Math.random()*1000}`)
        }
 
    </script>
</body>
</html>

效果

参考下图

 

 

 

 

说明

同时安利一个gif 图录屏好工具kap,很值得使用

参考资料

https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event
https://getkap.co/

posted on   荣锋亮  阅读(165)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-04-24 cube.js schemaVersion npm 包
2021-04-24 开发一个cube.js schemaVersion 包装
2021-04-24 cube.js 多租户参考设计说明
2020-04-24 sql server prometheus 监控
2020-04-24 apexsql 提供的一个sql server 方便的免费工具
2019-04-24 opendistro 试用
2019-04-24 Open Distro for Elasticsearch – How Different Is It?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示