在 HTML5 中,localStorage 对象是否按页面/域隔离?
HTML5 localStorage 对象是否按页面/域隔离?我想知道如何命名 localStorage 键。我需要单独的前缀吗?或者我可以随意命名它们吗?
Is the HTML5 localStorage object isolated per page/domain? I am wondering because of how I would name localStorage keys. Do I need a separate prefix? Or can I name them whatever I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它针对每个域和端口(与同源策略相同的隔离规则),要使其按页面显示,您必须使用基于
位置
的密钥或其他方法。您不需要前缀,但如果需要,请使用前缀。另外,是的,您可以随意命名它们。
It's per domain and port (the same segregation rules as the same origin policy), to make it per-page you'd have to use a key based on the
location
, or some other approach.You don't need a prefix, use one if you need it though. Also, yes, you can name them whatever you want.
商店按来源划分,其中“来源”与相同来源相同策略(架构 [
http
与https
等]、端口和主机的组合)。来自规范:因此,
http://a.example.com
的存储和http://b.example.com
的存储是分开的(并且它们都是分开的)来自http://example.com
),因为这些都是不同的主机。同样,http://example.com:80
和http://example.com:8080
和https://example.com
都是不同的起源。Web 存储中没有内置机制允许一个源访问另一个源的存储。
请注意,它是来源,而不是 URL,因此
http://example.com/page1
和http://example.com/page2
都是有权访问http://example.com
的存储。The stores are per origin, where "origin" is the same as for the Same Origin Policy (a combination of schema [
http
vs.https
, etc.], port, and host). From the spec:Thus, the storage for
http://a.example.com
and the storage forhttp://b.example.com
are separate (and they're both separate fromhttp://example.com
) as those are all different hosts. Similarly,http://example.com:80
andhttp://example.com:8080
andhttps://example.com
are all different origins.There is no mechanism built into web storage that allows one origin to access the storage of another.
Note that it's origin, not URL, so
http://example.com/page1
andhttp://example.com/page2
both have access to the storage forhttp://example.com
.是的,每个域/子域都有不同的 localStorage,您可以随意调用这些键(不需要前缀)。
要获取密钥,您可以使用方法 key(index) 例如
There was an object called globalStorage before you can have multiple localStorages, but it's be deprecated from the specs
Yeah, each domain/subdomain has a different localStorage and you can call the keys whatever you want (prefix is not required).
To get a key you can use the method key(index) such as
There was an object called globalStorage before where you could have multiple localStorages, but it's been deprecated from the specs
正如其他人指出的那样,localStorage 对于每个协议、主机和存储都是唯一的。港口。如果您想要一种方便的方法来使用前缀键控制存储,我建议 localDataStorage。
它不仅可以通过给键添加前缀来帮助在同一域内强制分段共享存储,还可以透明地存储 javascript 数据类型(数组、布尔值、日期、浮点型、整数、字符串和对象),提供轻量级数据混淆,自动压缩字符串,以及方便按键(名称)查询以及按(键)值查询。
[免责声明] 我是该实用程序的作者 [/免责声明]
示例:
如您所见,原始值受到尊重,并且您可以创建多个实例来控制存储。
As others have pointed out, localStorage is unique per protocol, host & port. If you want a handy way to control your storage with prefixed keys, I suggest localDataStorage.
Not only does it help enforce segmented shared storage within the same domain by prefixing keys, it also transparently stores javascript data types (Array, Boolean, Date, Float, Integer, String and Object), provides lightweight data obfuscation, automatically compresses strings, and facilitates query by key (name) as well as query by (key) value.
[DISCLAIMER] I am the author of the utility [/DISCLAIMER]
Examples:
As you can see, primitive values are respected, and you can create several instances to control your storage.
如果您想按页进行,我只需在键前添加 href 位置(为了更好的测量,我去掉了冒号和斜杠):
取自我的“恢复滚动位置”脚本的完整版本: https://stackoverflow.com/a/75359943/4885073
If you want to make it per page, I simply prefix the keys with the href location (I strip out the colons and slashes for good measure):
Taken from full version of my 'restore scroll position' script: https://stackoverflow.com/a/75359943/4885073
正如 Nick 建议的那样,它可以在该域的任何地方使用,作为替代方案,sessionStorage 的工作方式略有不同,因为它与浏览器窗口本身不同。也就是说,同一域上的其他选项卡或窗口无权访问存储对象的同一副本。
It is available anywhere on that domain as Nick suggested, as an alternative there is sessionStorage works slightly differently in that it is distinct to the browser window itself. That is to say that other tabs or windows on the same domain do not have access to that same copy of the storage object.