简体   繁体   中英

How can I store domain-wide persistent data with javascript?

I need a client-sided domain-wide way to store a string that is accessible after the page has been closed and reopened. I tried localStorage, but if I store data on www.example.com, I cannot access it from sub.example.com. In this case, redirecting the user to the root host is not an option. I'd really prefer to not send messages to an iframe to store it because it would require completely rewriting the current code I have. Is there a way to store data with something like localStorage, but access it from other subdomains?

My goal is to have two simple functions, get() and set(value) , that operate similar to how I've described.

Try storing the data in a cookie, and ensure the cookie is configured to support subdomains.

For example, ".example.com", with the leading ".", will make the cookie available to all subdomains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM