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

一世执手 提交于 2020-01-09 07:14:44

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16144906/is-it-possible-to-use-html5-local-storage-to-share-data-between-pages-from-diffe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!