localstorage跨域解决方案
localstorage也存在 跨域的问题,
【解决思路如下】
在A域和B域下引入C域,所有的读写都由C域来完成,本地数据存在C域下;
因此 A哉和B域的页面必定要引入C域的页面; 当然C域最好是主域,原因后面会提到(在localstorage 不方便的情况下使用cookie);
【A域】【B域】需要读写时,通过postMessage 向【C域】发送跨哉消息,
【C域】监听跨域消息,在接到批定的消息时进行读写操作,
【C域】接到跨域消息时,如果是写入删除可以不做什么,如果是读取,就要先读取本域本地数据通过postMessage向父页面发送消息,
【A 域 / B 域】在读取【C域】数据时就需要监听来自【C域】的跨域消息
【注意事项】
window.postMessage()方法,向【C域】发消息,应用iframe.contentWindow.postMessage() 这样iframe内的【C 域】才可以接到,
同理,【C域】向 【A 域B域】发消息时应用,window.parent.postMessage()
【A域、B域】的逻辑一定要在iframe 加载完成后进行
【C域】的页面如下:
根据域名请自行修改
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "robots" content= "noindex" > <title>cross domain</title> </head> <body> <script> ;( function (doc,win,undefined){ var fn= function (){}; fn.prototype={ /*本地数据存储*/ setLocalCookie: function (k, v, t,domain) { typeof window.localStorage !== "undefined" ? localStorage.setItem(k, v) : ( function () { t = t || 365 * 12 * 60 * 60; domain=domain?domain: ".hc360.com" ; document.cookie = k + "=" + v + ";max-age=" + t+ ";domain=" +domain+ ";path=/" ; })() }, getLocalCookie: function (k) { k = k || "localDataTemp" ; return typeof window.localStorage !== "undefined" ? localStorage.getItem(k) : ( function () { var all = document.cookie.split( ";" ); var cookieData = {}; for ( var i = 0, l = all.length; i < l; i++) { var p = all[i].indexOf( "=" ); var dataName = all[i].substring(0, p).replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "" ); cookieData[dataName] = all[i].substring(p + 1); } return cookieData[k] })(); }, clearLocalData: function (k) { k = k || "localDataTemp" ; typeof window.localStorage !== "undefined" ? localStorage.removeItem(k) : ( function () { document.cookie = k + "=temp" + ";max-age=0" ; })() }, init: function (){ this .bindEvent(); }, bindEvent: function (){ var _this= this ; win.addEventListener( "message" , function (evt){ if (win.parent!=evt.source){ return } var options=JSON.parse(evt.data); if (options.type== "GET" ){ var data=tools.getLocalCookie(options.key); win.parent.postMessage(data, "*" ); } options.type== "SET" &&_this.setLocalCookie(options.key,options.value); options.type== "REM" &&_this.clearLocalData(options.key); }, false ) } }; var tools= new fn(); tools.init(); })(document,window); </script> </body> </html> |
【A域、B域 读取操作】
【写】
iframe.contentWindow.postMessage(JSON.stringify({type: "SET" ,key: "key" ,value: "value" }), 'http://www.C.com' ); |
【读】
1 | iframe.contentWindow.postMessage(JSON.stringify({type: "GET" ,key: "key" }), 'http://www.C.com' ); |
【删】
1 | iframe.contentWindow.postMessage(JSON.stringify({type: "REM" ,key: "key" }), 'http://www.C.com' ); |
---------------------
原文:https://blog.csdn.net/sflf36995800/article/details/53290457
分类:
Javascript
, HTML5
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具