Let’s say I have a website : https://website.com
This website has two pages : https://website.com/page1 https://website.com/page2
On the page1, I have some JavaScript writing and reading data in the the LocalStorage, let’s say ["myKey" => "myValue"].
Is there a way to restrain access to "myKey" to only page1, so that page2 or any other page on this website can’t read nor write this data ? Maybe a parameter when calling localStorage.setItem() ?
(I know that localStorage data access is scoped on the website, so by default in this scenario "page2" has access to it, but this is what I want to prevent)
I looked online but nobody seemed to have encountered this problem.
CodePudding user response:
Unfortunately, there is no built-in way to restrict access to a specific key in LocalStorage to only one page on a website.
One solution to this problem would be to use a different key for each page. You could, for example, use a prefix for each key that corresponds to the page where it is being used, such as "page1_myKey" and "page2_myKey". This way, you could easily identify which keys belong to which page, and you could check the prefix before accessing the key's value.