除了非持久化和仅作用于当前窗口之外,会话存储相对于本地存储是否有任何好处(性能,数据访问等)?
当前回答
The only difference is that localStorage has a different expiration time, sessionStorage will only be accessible while and by the window that created it is open. localStorage lasts until you delete it or the user deletes it. Lets say that you wanted to save a login username and password you would want to use sessionStorageover localStorage for security reasons (ie. another person accessing their account at a later time). But if you wanted to save a user's settings on their machine you would probably want localStorage. All in all:
localStorage—用于长期使用。 sessionStorage—当您需要存储更改的东西或临时的东西时使用
其他回答
sessionStorage与localStorage相同,只是它只存储一个会话的数据,并且当用户关闭创建它的浏览器窗口时,它将被删除。
其他几点可能有助于理解本地存储和会话存储之间的区别
Both local storage and session storage are scoped to document origin, so https://mydomain.example/ http://mydomain.example/ https://mydomain.example:8080/ All of the above URL's will not share the same storage. (Notice path of the web page does not affect the web storage) Session storage is different even for the document with same origin policy open in different tabs, so same web page open in two different tabs cannot share the same session storage. Both local and session storage are also scoped by browser vendors. So storage data saved by IE cannot be read by Chrome or FF.
The only difference is that localStorage has a different expiration time, sessionStorage will only be accessible while and by the window that created it is open. localStorage lasts until you delete it or the user deletes it. Lets say that you wanted to save a login username and password you would want to use sessionStorageover localStorage for security reasons (ie. another person accessing their account at a later time). But if you wanted to save a user's settings on their machine you would probably want localStorage. All in all:
localStorage—用于长期使用。 sessionStorage—当您需要存储更改的东西或临时的东西时使用
本地存储:保存没有过期日期的用户信息数据,当用户关闭浏览器窗口时,这些数据不会被删除,它将在天,周,月和年可用。
//Set the value in a local storage object
localStorage.setItem('name', myName);
//Get the value from storage object
localStorage.getItem('name');
//Delete the value from local storage object
localStorage.removeItem(name);//Delete specifice obeject from local storege
localStorage.clear();//Delete all from local storege
会话存储:它与本地存储日期相同,只是当web用户关闭浏览器窗口时,它会删除所有窗口。
//set the value to a object in session storege
sessionStorage.myNameInSession = "Krishna";
阅读更多点击
sessionStorage为每个给定的源维护一个单独的存储区域,该存储区域在页面会话期间可用(只要浏览器打开,包括页面重新加载和恢复)。 localStorage做同样的事情,但是即使浏览器关闭并重新打开,它仍然存在。
我从Web存储API中获得了这个
推荐文章
- jQuery中的live()转换为on()
- 如何区分鼠标的“点击”和“拖动”
- IE9是否支持console.log,它是一个真实的功能吗?
- Node.js同步执行系统命令
- 如何转义JSON字符串包含换行字符使用JavaScript?
- 画布在使用CSS时被拉伸,但正常使用“width”/“height”属性
- jQuery等价于JavaScript的addEventListener方法
- jQuery需要避免的陷阱
- JavaScript中变量字符串的XML解析
- 'React'指的是一个UMD全局,但当前文件是一个模块
- 为什么useState不触发重新渲染?
- 如何使用回调与useState挂钩在反应
- 网络请求失败
- 如何使用JavaScript大写字符串中每个单词的第一个字母?
- 如何使用箭头函数(公共类字段)作为类方法?