skip to Main Content

How can I use local storage? setItem in one application, and how can I get local storage? getItem in another application

1.Application

let data="This is Session"
localstorage.setItem("myurl",data)

2.Application

const newdata = localstorage.getItem("myurl")

I am expecting This is session, but it’s getting null. How can I solve the problem?

3

Answers


  1. localStorage is subject to the same-origin policy, which means that data stored in localStorage is accessible only to web pages from the same domain and protocol (e.g., http://example.com). Different applications typically have different domains or protocols, which would prevent them from sharing data in localStorage.

    You cannot access items from localstorage of other domains.

    Login or Signup to reply.
  2. You can’t, because, I think, the localstorage is restricted by the hostname, protocol and port.

    But maybe you can try this hack : webmessaging :

    https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
    http://www.w3.org/TR/webmessaging/

    Login or Signup to reply.
  3. As stated in MDN:

    The localStorage read-only property of the window interface allows you
    to access a Storage object for the Document’s origin; the stored data
    is saved across browser sessions.

    Therefore, no, you cannot, unless you use the same domain and port. However, what you could do is use cookies, as they were designed for cross-application usage.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search