Can HTML5 local storage be used to exchange data between pages from different sites?

I would like to create data on the user side and allow javascript from another URL to access it. I know about the same origin policy, but I was wondering if some exceptions could be created. Or is there a trick / feature that I could use?

+4
source share
1 answer

The best trick I know is to use iframes and the postMessage API to access localStorage from an external domain.

This technique is pretty simple:

  • on your page, you must create an iframe for the domain from which you want to receive data.
  • Your data domain needs to listen to the message event:

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

  • the handler will be responsible for accessing localStorage and sending its contents to the source domain

  • your source domain can call the handler function in the data area using the postMessage API https://developer.mozilla.org/en-US/docs/DOM/window.postMessage

To ensure the security of your data, you can use the X-Frame-Options ALLOW-FROM uri HTTP header https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options?redirectlocale=en-US&redirectslug= The_X-FRAME-OPTIONS_response_header

Hope this helps.

+9
source

Source: https://habr.com/ru/post/953529/


All Articles