每个iframe是否都存储html5本地存储? [英] Does html5 local storage store per iframe?

查看:192
本文介绍了每个iframe是否都存储html5本地存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有A页,并且它加载了多个iframe B,C,D,E,F.并且在每个iframe中,都有javascript代码在本地存储中存储了值为red的键color.所有iframe本地存储会冲突还是每个iframe独立?如果它不是独立的,是否有办法使其独立?

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?

谢谢

推荐答案

根据 W3C :

本地存储是每个来源的(每个域和协议).所有页面,从一个来源出发,都可以存储和访问相同的数据.

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

换句话说,如果所有iframe都位于同一域中,则它们将共享同一本地存储实例.

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

一种可能避免冲突的方法是将当前页面URL用作本地存储字典的键,例如:

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

这篇关于每个iframe是否都存储html5本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆