gpt4 book ai didi

javascript - LocalStorage 是应用程序常用的,那么如何避免不小心删除值呢?

转载 作者:行者123 更新时间:2023-11-29 19:08:41 25 4
gpt4 key购买 nike

LocalStorage 通常由应用程序使用。这是否意味着您的值可能会被其他应用程序操纵或删除?或者是否使用了某种命名空间,以便将值存储在每个域的基础上,而不会有被干扰的风险?提示将不胜感激。

最佳答案

localStorage 对于每个源都是唯一的,因此它 是安全的,并且不能被其他网站或应用程序干扰。

spec具体来说

The localStorage object provides a Storage object for an origin.
User agents must have a set of local storage areas, one for each origin.

User agents should expire data from the local storage areas only for security reasons or when requested to do so by the user.
User agents should always avoid deleting data while a script that could access that data is running.

When the localStorage attribute is accessed, the user agent must run the following steps, which are known as the Storage object initialization steps:

  1. The user agent may throw a "SecurityError" DOMException and abort these steps instead of returning a Storage object if the request violates a policy decisions (e.g. if the user agent is configured to not allow the page to persist data).

  2. If the Document's origin is an opaque origin, then throw a "SecurityError" DOMException and abort these steps.

  3. Check to see if the user agent has allocated a local storage area for the origin of the Document of the Window object on which the attribute was accessed.
    If it has not, create a new storage area for that origin.

  4. Return the Storage object associated with that origin's local storage area.
    Each Document object must have a separate object for its Window's localStorage attribute.

请注意 "an origin"是相当明确的定义,但对于本地存储,这意味着协议(protocol)、域和端口必须匹配,两个端点才能被视为同源

关于javascript - LocalStorage 是应用程序常用的,那么如何避免不小心删除值呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com