David
David

Reputation: 309

Can I set local storage item in parent window?

Initial page is on port 3001 (no server, CRA frontend) there one can open a pop-up window that points to 3000 port (Rails server).

I'm trying to set localStorage value in parent window (opener).

<<-HEREDOC
  window.opener.localStorage.setItem('authorization', 'Bearer #{@jwt_token}');
  window.close();
HEREDOC

But I'm getting

enter image description here

Do you think there's a way I can add storage value from popup to opener?

Upvotes: 3

Views: 2915

Answers (1)

Randy Casburn
Randy Casburn

Reputation: 14175

Here is a simple example from MDN that shows how to implement Cross Document Messaging. It is your use case as far as I can tell:

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#Example

Here is the relevant quote that explains why this alternative is important to consider:

The window.postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.

Upvotes: 3

Related Questions