简体   繁体   English

是否可以使用HTML5本地存储在不同站点的页面之间共享数据?

[英]Is it possible to use HTML5 local storage to share data between pages from different sites?

I would like to create data on the user side and let javascript from another URL access it too. 我想在用户端创建数据,让其他URL的javascript也可以访问它。 I am aware of the same origin policy, but I was wondering whether it is possible to create some exceptions. 我知道相同的原始政策,但我想知道是否有可能创建一些例外。 Or, is there any trick/feature I could use? 或者,我可以使用任何技巧/功能吗?

Best trick I know is to use iframes and postMessage API do get access to localStorage from external domain. 我知道的最好的技巧是使用iframe和postMessage API从外部域访问localStorage。

This technique is quite simple: 这种技术很简单:

  • on you page you must create iframe to a domain from which you want to get data 在您的页面上,您必须为要从中获取数据的域创建iframe
  • your data domain need listen to message event: 您的数据域需要侦听message事件:

    document.addEventListener ("message", handler, useCapture); document.addEventListener(“message”,handler,useCapture);

  • handler will be responsible for accessing localStorage and posting its content to source domain handler将负责访问localStorage并将其内容发布到源域

  • your source domain may call handler function on data domain with postMessage API https://developer.mozilla.org/en-US/docs/DOM/window.postMessage 您的源域可以使用postMessage API调用数据域上的handler函数https://developer.mozilla.org/en-US/docs/DOM/window.postMessage

For security of your data you can use HTTP header X-Frame-Options ALLOW-FROM uri https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options?redirectlocale=en-US&redirectslug=The_X-FRAME-OPTIONS_response_header 为了保护您的数据,您可以使用HTTP标头X-Frame-Options ALLOW-FROM uri https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options?redirectlocale=en-US&redirectslug=The_X -frame-OPTIONS_response_header

Hope it will help. 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用任何HTML5功能将本地存储导出到Excel? - Is it possible to use any HTML5 fanciness to export local storage to Excel? 是否可以从HTML5的本地存储中提取数据并保存到服务器数据库? - Is it possible to pull data from HTML5's local storage and save to server database? indexedDB在概念上与HTML5本地存储有何不同? - How is indexedDB conceptually different from HTML5 local storage? 在html页面之间共享数据 - Share data between html pages 如何使用 HTML5 本地存储保存表单中的数据? - How to save data from a form with HTML5 Local Storage? 如何防止在同一容器中部署的两个或多个不同应用程序之间传递HTML5本地存储数据? - How to prevent passing HTML5 local storage data between two or more different applications which were deployed at the same container? 如何使用单个Javascript文件在两个或更多HTML页面之间共享本地存储,而不会出现“空”的情况? - How to share local storage between two or more HTML pages using a single Javascript file and not getting “null”? 在本地存储中保留HTML5数据属性 - Persist HTML5 data attributes in local storage 数据不使用本地存储存储吗? -HTML5 - Data is not storing using local Storage ? - Html5 是否可以在HTML5中单独覆盖本地存储和会话存储? - Is it possible to override Local Storage and Session Storage separately in HTML5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM