是否可以使用HTML5本地存储在不同网站的页面之间共享数据? [英] Is it possible to use HTML5 local storage to share data between pages from different sites?
问题描述
我想在用户端创建数据,并让其他网址的JavaScript也可以访问它。我知道相同的原产地政策,但我想知道是否有可能创造一些例外情况。或者,是否有任何可以使用的技巧/功能? 解决方案我知道的最佳技巧是使用iframe和 postMessage
API可以从外部域访问localStorage。
这个技巧很简单:
- 您必须将iframe创建到您要从中获取数据的域
-
您的数据域需要收听
message
event:
document.addEventListener(message,handler,useCapture);
li>
-
处理程序将负责访问
localStorage
并将其内容发布到源域中 - 您的源域可能会使用
postMessage
API
处理函数函数= https://developer.mozilla.org/en-US/docs/DOM/window.postMessage> https://developer.mozilla.org/en-US/docs/DOM/window.postMessage
为了您的数据的安全性,您可以使用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 withpostMessage
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屋!