Javascript 什么是 window.localStorage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10059755/
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 23:40:41  来源:igfitidea点击:

What is window.localStorage

javascripthtmllocal-storage

提问by Siddhpura Amit

The option.jsfile of "Email this page"(Chrome extension example) contains the following code:

option.js文件发送本页面(Chrome扩展例子)包含下面的代码:

if (window.localStorage == null) {
  ...
if (window.localStorage.customMailtoUrl == null) {

What does this mean? What is window.localStorage?

这是什么意思?什么是window.localStorage

回答by Aaron

localStorage/sessionStorage is part of HTML5 API. Essentially, this is what cookies are used for. But this is a lot better.

localStorage/sessionStorage 是 HTML5 API 的一部分。本质上,这就是 cookie 的用途。但这要好得多。

https://developer.mozilla.org/en/DOM/Storage

https://developer.mozilla.org/en/DOM/Storage

回答by Manuel Alves

From http://www.w3schools.com/html/html5_webstorage.asp:

http://www.w3schools.com/html/html5_webstorage.asp

"What is HTML Local Storage?

“什么是 HTML 本地存储?

With local storage, web applications can store data locally within the user's browser.

通过本地存储,Web 应用程序可以在用户浏览器中本地存储数据。

Before HTML5, application data had to be stored in cookies, included in every server request. Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

在 HTML5 之前,应用程序数据必须存储在 cookie 中,包含在每个服务器请求中。本地存储更安全,可以在本地存储大量数据,不影响网站性能。

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

与 cookie 不同的是,存储限制要大得多(至少 5MB)并且信息永远不会传输到服务器。

Local storage is per domain. All pages, from one domain, can store and access the same data."

本地存储是每个域。来自一个域的所有页面都可以存储和访问相同的数据。”

回答by sky

localStorageis a property of HTML5 API which allows web applications to store data locally within the user's browser.

localStorage是 HTML5 API 的一个属性,它允许 Web 应用程序在用户浏览器中本地存储数据。

Before HTML5, application data had to be stored in cookies, included in every server request. localStorage is a different property which has many advantages over cookies.

在 HTML5 之前,应用程序数据必须存储在 cookie 中,包含在每个服务器请求中。localStorage 是一个不同的属性,它比 cookie 有很多优势。

HTML local storage provides two objects for storing data on the client:

HTML 本地存储提供了两个对象用于在客户端存储数据:

window.localStorage- stores data with no expiration date

window.localStorage- 存储没有到期日期的数据

window.sessionStorage- stores data for one session (data is lost when the browser tab is closed)

window.sessionStorage- 存储一个会话的数据(关闭浏览器选项卡时数据丢失)

localStorage features:

本地存储功能:

  • The localStorage data has no expiration date.
  • localStorage is more secure since it doesn't send anything to server, everything happens at client side i.e. browser.
  • localStorage is per origin means two or more html pages, from one origin, can store and access the same data stored in localStorage Object.
  • It can store much more data than cookies. The storage size varies for each browser for example the latest versions of chrome & firefox can store atleast 5MB of data.
  • localStorage 数据没有过期日期。
  • localStorage 更安全,因为它不向服务器发送任何内容,一切都发生在客户端,即浏览器。
  • localStorage is per origin 意味着来自一个源的两个或多个 html 页面可以存储和访问存储在 localStorage 对象中的相同数据。
  • 它可以存储比 cookie 多得多的数据。每个浏览器的存储大小不同,例如最新版本的 chrome 和 firefox 可以存储至少 5MB 的数据。