cross domain localstorage with javascript

后端 未结 4 1715
挽巷
挽巷 2020-12-09 03:11

We have a javascript api.js which is hosted on domain api.abc.com. It manages the local storage.

We included this javascript in our websites at abc.com and login.abc

4条回答
  •  旧时难觅i
    2020-12-09 03:26

    The other answers all ignore the fact that you're not really operating cross-domain, just between subdomains.

    You still need a hidden iframe to encapsulate the origin of the localStorage store you want to access (api.abc.com), but by setting document.domain = "abc.com" on both main window and hidden iframe, they can interact directly.

    Then you can literally just use hiddenIFrame.contentWindow.localStorage instead of window.localStorage, and forget about the headache of doing anything via postMessage().

    I posted a more detailed version of this answer here: https://stackoverflow.com/a/63602446/999120

提交回复
热议问题