最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does html5 local storage store per iframe? - Stack Overflow

programmeradmin9浏览0评论

If I have a page A, and it loads several iframes B,C,D,E,F. And in each iframe, there is javascript code that stores a key color with value red in local storage. Will all the iframes local storage collide or is each one independent? If it is not independent, is there a way to make it independent?

Thanks

If I have a page A, and it loads several iframes B,C,D,E,F. And in each iframe, there is javascript code that stores a key color with value red in local storage. Will all the iframes local storage collide or is each one independent? If it is not independent, is there a way to make it independent?

Thanks

Share Improve this question asked Nov 30, 2015 at 15:26 omegaomega 43.9k89 gold badges285 silver badges520 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 24

According to the W3C:

Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.

In other words, if the iframes all reside in the same domain, then they will share the same local storage instance.

One way to potentially avoid collisions would be to use the current page URL as a key into a local storage dictionary, something like:

localStorage.setItem(window.location.pathname + "_color", "red");
发布评论

评论列表(0)

  1. 暂无评论