如果我有一个页面 A,它加载了多个 iframe B、C、D、E、F。在每个 iframe 中,都有 javascript 代码在本地存储中存储键 color
和值 red
。所有的 iframe 本地存储会冲突还是每个都是独立的?如果不独立,有没有办法让它独立?
谢谢
最佳答案
根据 the W3C :
Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.
换句话说,如果 iframe 都驻留在同一个域中,那么它们将共享同一个本地存储实例。
一种可能避免冲突的方法是使用当前页面 URL 作为本地存储字典的键,例如:
localStorage.setItem(window.location.pathname + "_color", "red");
关于javascript - html5 本地存储是否按 iframe 存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002044/