Javascript HTML5 本地存储与会话存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5523140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:37:14  来源:igfitidea点击:

HTML5 Local storage vs. Session storage

javascripthtmllocal-storagesession-storage

提问by jpkeisala

Apart from being non persistent and scoped only to the current window, are there any benefits (performance, data access, etc) to Session Storage over Local Storage?

除了非持久性和仅限于当前窗口之外,会话存储相对于本地存储是否有任何好处(性能、数据访问等)?

回答by tcooc

localStorageand sessionStorageboth extend Storage. There is no difference between them except for the intended "non-persistence" of sessionStorage.

localStoragesessionStorage都扩展了Storage。除了预期的“非持久性”之外,它们之间没有区别sessionStorage

That is, the data stored in localStoragepersists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.

也就是说,存储在 中的数据localStorage一直存在,直到明确删除。所做的更改将被保存并可供所有当前和将来访问该站点时使用。

For sessionStorage, changes are only available per tab.Changes made are saved and available for the current page in that tabuntil it is closed. Once it is closed, the stored data is deleted.

对于sessionStorage更改仅适用于每个选项卡。所做的更改将被保存并可用于该选项卡中的当前页面直到它关闭。一旦关闭,存储的数据将被删除。

回答by MayorMonty

The only difference is that localStorage has a different expiration time, sessionStoragewill only be accessible while and by the window that created it is open.
localStoragelasts 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 localStoragefor 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只能在创建它的窗口打开时访问。
localStorage一直持续到您删除它或用户删除它为止。
假设您想保存一个登录用户名和密码sessionStoragelocalStorage出于安全原因(即其他人稍后访问他们的帐户),您想使用它。
但是,如果您想将用户的设置保存在他们的机器上,您可能需要localStorage. 总而言之:

localStorage- use for long term use.
sessionStorage- use when you need to store somthing that changes or somthing temporary

localStorage- 长期使用。
sessionStorage- 当您需要存储更改或临时存储的东西时使用

回答by functionoid

Few other points which might be helpful to understand differences between local and session storage

可能有助于理解本地和会话存储之间差异的其他几点

  1. Both local storage and session storage are scoped to document origin, so

    https://mydomain.com/
    http://mydomain.com/
    https://mydomain.com:8080/

    All of the above URL's will notshare the same storage. (Notice path of the web page does not affect the web storage)

  2. 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 cannotshare the same session storage.

  3. Both local and session storage are also scoped by browser vendors. So storage data saved by IE cannot be read by Chrome or FF.

  1. 本地存储和会话存储都限于文档来源,因此

    https://mydomain.com/
    http://mydomain.com/
    https://mydomain.com:8080/

    所有上述 URL不会共享相同的存储。(注意网页路径不影响网页存储)

  2. 即使在不同标签页中打开具有同源策略的文档,会话存储也是不同的,因此在两个不同标签页中打开的同一个网页不能共享同一个会话存储。

  3. 本地和会话存储也受浏览器供应商的限制。所以IE保存的存储数据不能被Chrome或FF读取。

Hope this helps.

希望这可以帮助。

回答by Eek

The main difference between localStorageand sessionStorageis that sessionStorageis unique per tab. If you close the tab the sessionStoragegets deleted, localStoragedoes not. Also you cannot communicate between tabs :)

之间的主要区别localStoragesessionStoragesessionStorage为每个标签是唯一的。如果您关闭选项卡,则会sessionStorage被删除,localStorage不会。您也无法在选项卡之间进行通信:)

Another subtle difference is that for example on Safari (8.0.3) localStoragehas a limit of 2551 k characters but sessionStoragehas unlimited storage

另一个细微的区别是,例如在 Safari (8.0.3) 上localStorage有 2551 k 个字符的限制,但sessionStorage无限的存储空间

On Chrome (v43) both localStorageand sessionStorageare limited to 5101 k characters (no difference between normal / incognito mode)

在Chrome(V43)都localStoragesessionStorage被限制为5101个ķ字符(正常/无痕模式之间没有差别)

On Firefox both localStorageand sessionStorageare limited to 5120 k characters (no difference between normal / private mode )

在 FirefoxlocalStoragesessionStorage都被限制为 5120 k 个字符(正常/私人模式之间没有区别)

No difference in speed whatsoever :)

速度没有任何区别:)

There's also a problem with Mobile Safari and Mobile Chrome, Private Mode Safari & Chrome have a maximum space of 0KB

Mobile Safari 和 Mobile Chrome 也有问题,Private Mode Safari & Chrome 的最大空间为 0KB

回答by Ahmad Santarissy

sessionStorageis the same as localStorage, except that it stores the data for only one session, and it will be removed when the user closes the browser window that created it.

sessionStorage与 相同localStorage,不同之处在于它只存储一个会话的数据,当用户关闭创建它的浏览器窗口时,它将被删除。

回答by cc young

performance wise, my (crude) measurements found no difference on 1000 writes and reads

性能方面,我的(粗略)测量发现 1000 次写入和读取没有差异

security wise, intuitively it would seem the localStore might be shut down before the sessionStore, but have no concrete evidence - maybe someone else does?

安全明智,直觉上似乎 localStore 可能在 sessionStore 之前关闭,但没有具体证据 - 也许其他人这样做了?

functional wise, concur with digitalFresh above

功能明智,同意上面的digitalFresh

回答by Bhargavi

Ya session storage and local storage are same in behaviour except one that is local storage will store the data until and unless the user delete the cache and cookies and session storage data will retain in the system until we close the session i,e until we close the session storage created window.

雅会话存储和本地存储在行为上是相同的,除了本地存储将存储数据直到并且除非用户删除缓存和 cookie 和会话存储数据将保留在系统中,直到我们关闭会话,即直到我们关闭会话存储创建窗口。

回答by avvett

The advantage of the session storage over local storage, in my opinion, is that it has unlimited capacityin Firefox, and won't persist longer than the session. (Of course it depends on what your goal is.)

在我看来,会话存储相对于本地存储的优势在于它在 Firefox 中具有无限容量,并且持续时间不会超过会话。(当然这取决于你的目标是什么。)

回答by Srikrushna

Local storage:It keeps store the user information data without expiration date this data will not be deleted when user closed the browser windows it will be available for day, week, month and year.

本地存储:保存用户信息数据,无过期日期,用户关闭浏览器窗口时不会删除该数据,日、周、月、年均可使用。

//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

Session Storage:It is same like local storage date except it will delete all windows when browser windows closed by a web user.

会话存储:它与本地存储日期相同,但它会在网络用户关闭浏览器窗口时删除所有窗口。

//set the value to a object in session storege
sessionStorage.myNameInSession = "Krishna";

Read More Click

阅读更多点击

回答by Linda Gutiérrez Montoya

  • sessionStoragemaintains a separate storage area for each given origin that's available for the duration of the page session (as long as the browser is open, including page reloads and restores)

  • localStoragedoes the same thing, but persists even when the browser is closed and reopened.

  • sessionStorage为每个给定的源维护一个单独的存储区域,在页面会话期间可用(只要浏览器打开,包括页面重新加载和恢复)

  • localStorage做同样的事情,但即使在浏览器关闭并重新打开时仍然存在。

I took this from https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API

我从https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API