Does html5 local storage store per iframe?

拈花ヽ惹草 提交于 2020-07-18 09:14:19

问题


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


回答1:


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");


来源:https://stackoverflow.com/questions/34002044/does-html5-local-storage-store-per-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!