是否可以使用HTML5本地存储在不同网站的页面之间共享数据? [英] Is it possible to use HTML5 local storage to share data between pages from different sites?

查看:540
本文介绍了是否可以使用HTML5本地存储在不同网站的页面之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户端创建数据,并让其他网址的JavaScript也可以访问它。我知道相同的原产地政策,但我想知道是否有可能创造一些例外情况。或者,是否有任何可以使用的技巧/功能? 解决方案我知道的最佳技巧是使用iframe和 postMessage API可以从外部域访问localStorage。



这个技巧很简单:





为了您的数据的安全性,您可以使用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



希望它会有所帮助。

I would like to create data on the user side and let javascript from another URL access it too. 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.

This technique is quite simple:

  • on you page you must create iframe to a domain from which you want to get data
  • your data domain need listen to message event:

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

  • handler will be responsible for accessing localStorage and posting its content to source domain

  • your source domain may call handler function on data domain with postMessage API 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

Hope it will help.

这篇关于是否可以使用HTML5本地存储在不同网站的页面之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆