Is it possible to use localstorage with context per path like cookie?

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-02 20:28:51

问题


Aware of issues like:

  • Cookies are included with every HTTP request, thereby slowing down your web application by needlessly transmitting the same data over and over
  • Cookies are included with every HTTP request, thereby sending data unencrypted over the internet (unless your entire web application is served over SSL)
  • Cookies are limited to about 4 KB of data — enough to slow down your application (see above), but not enough to be terribly useful

(from: http://diveintohtml5.info/storage.html)

I decided to use localstorage instead of cookies.

QUESTION: Can I limit localstorage context to certain path like with cookies?


回答1:


As mentioned in the comments already, webstorages (localStorage, sessionStorage) are scoped per origin.

User agents must have a set of local storage areas, one for each origin.

Source: http://www.w3.org/TR/webstorage/#the-localstorage-attribute


What is an origin?

Let's just quote the w3.org wiki page on the same origin policy:

An origin is defined by the scheme, host, and port of a URL.

Source: http://www.w3.org/Security/wiki/Same_Origin_Policy




回答2:


Use some fresh Library!

https://www.npmjs.com/package/pathstorage

I was agony for the same subject. And I choose solution "made my own". :)



来源:https://stackoverflow.com/questions/25013643/is-it-possible-to-use-localstorage-with-context-per-path-like-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!