Iframe chrome : Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document

Viewed 4790

I have created a JavaScript widget. I have used localstorage for setting and getting properties of the window.

When I open that widget in Chrome it throws an error:

Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.

If I allow third party cookies in the browser it's working properly. The problem is that I cannot allow third parties cookies of every user manually. What is a solution to this?

1 Answers

You should consider to use this browser API instead:

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

Since the local storage is only related to the specific domain, so it can not be used across different domains.

For example: if the iframe domain is "www.aaa.com" and page domain is "www.bbb.com", the iframe can not access to the page related local storage. No problem if they are in the same domain.

A complete explanation about this argument can be found here:

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Related