会话存储和本地存储的范围

我阅读了一些关于 sessionStorage 和 localStorage 的文档,但是我不明白它们的作用域是什么: 域还是特定的页面?

例如,如果我有以下页面:

Http://example.com/products.aspx?productid=1

Http://example.com/products.aspx?productid=2

Http://example.com/services.aspx?serviceid=3

如果在上面的每个页面上运行(idvalue 是 querystring 中的值) :

localStorage.setItem('ID',idvalue);

我最终是要存储3个不同的值,还是这些值会相互重写?

94931 次浏览

The values are going to overwrite each other. Each key-name pair is unique for a protocol and domain, regardless of the paths.

The affected domain can be changed via the document.domain property.

  • sub.example.com -> example.com is possible (subdomain)
  • sub.example.com -> other.example.com is not possible

Session Storage:

  1. Values persist only as long as the window or tab in which they stored.

  2. Values are only visible within the window or tab that created them.

Local Storage:

  1. Values persist window and browser lifetimes.

  2. Values are shared across every window or tab running at the same origin.

So, by reading and understanding this each key-value pair is unique for each domain, because local storage persist values across window or tab.