什么是localStorage? [英] What is localStorage?

查看:116
本文介绍了什么是localStorage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释 localStorage 是什么,以及我将如何或在何处使用它?

Could someone please explain to me what localStorage is, and how or where I would use it?

我意识到这个问题可以通过谷歌搜索来回答,但我更喜欢简洁,易于查找,良好的索引和我从Stack Overflow获得的各种不同见解答案。

I realise this question can be answered by "Googling", but I prefer the concise, easy to find, well index & diverse opinionated answers that I get from Stack Overflow.

推荐答案

localStorage是一种在客户端计算机上存储数据的方法。假设您要保存上次用户访问您网页时的日期。页面加载后,您可以使用以下代码:

localStorage is a way to store data on the client's computer. Let's say you want to save the date the last time a user visited your webpage. You can use the following code once the page loads:

function saveData() {
  localStorage.lasttimevisited = new Date();
}

下次加载页面时,您可以检查 localStorage.lasttimevisited 已满,如果是,欢迎用户。

The next time the page loads you can check if localStorage.lasttimevisited is full and if it is, welcome the user.

localStorage的优势在于它仍然在内存中即使你关闭了浏览器。如果有人访问该页面,他们会受到上次欢迎(如果他们上次访问过),即使他们上次访问的时间是几个世纪以前。

The advantages to localStorage is that it will still be in memory even when you close the browser. If somebody visits that page, they will get a welcome from last time (if they visited last time) even if the last time they visited was centuries ago.

但是,有一些缺点。用户可以清除浏览器数据/缓存以清除所有localStorage数据。他们也可以使用不受支持的浏览器,例如IE7。

However, there are some disadvantages. The user can clear the browser data/cache to erase all localStorage data. They can also have an unsupported browser, such as IE7.

localStorage只能通过JavaScript访问,并且是HTML5。

localStorage can only be accessed via JavaScript, and is HTML5.

这篇关于什么是localStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆