How to implement local storage in javascript

PHPz
Release: 2023-04-25 13:46:55
Original
1223 people have browsed it

With the rapid development of the Internet, more and more websites adopt front-end page design, and with it comes the need for data storage. Generally speaking, we use cookies or localStorage in front-end code for data storage. This article will introduce how to implement local storage in JavaScript.

First, let’s take a look at the differences and connections between cookies and localStorage. Cookies are usually used to store smaller amounts of data, such as user login information, etc., while localStorage is a new standard in HTML5 and is used to store larger amounts of data, such as users' local settings, website configuration information, etc. At the same time, cookies are easily hijacked, but localStorage is more secure and reliable, so we can make reasonable choices according to our needs.

Next, let’s take a look at how to use JavaScript to implement localStorage operations.

1. Write data

We can use localStorage.setItem(key, value) to write data to and from localStorage, where key represents the key name and value represents the key value. Here is an example:

localStorage.setItem('name', '张三');
localStorage.setItem('age', '18');
Copy after login

The above code will store key values ​​with key names name and age respectively in local storage.

2. Read data

We can use localStorage.getItem(key) to read the key value of the specified key name in localStorage. Here is an example:

var name = localStorage.getItem('name');
var age = localStorage.getItem('age');
console.log(name, age);
Copy after login

The above code will output the key values ​​of name and age respectively.

3. Delete data

We can use localStorage.removeItem(key) to delete the data with the specified key name in localStorage. Here is an example:

localStorage.removeItem('name');
Copy after login

The above code will delete the data with the key name name in the local storage.

4. Clear data

We can use localStorage.clear() to clear all data in local storage. Here is an example:

localStorage.clear();
Copy after login

The above code will clear all data in local storage.

5. Detect whether localStorage is supported

We can use the following code to detect whether the current browser supports localStorage:

if (window.localStorage) {
  console.log('localStorage is supported');
} else {
  console.log('localStorage is not supported');
}
Copy after login

If the browser supports localStorage, output "localStorage is supported"; otherwise, "localStorage is not supported" is output.

6. Set the data expiration time

localStorage does not support setting the data expiration time by default, but we can implement this function by writing our own code, for example:

// 存储数据,同时存储当前时间戳
localStorage.setItem('name', '张三');
localStorage.setItem('name_time', Date.now());

// 读取数据,同时读取存储的时间戳,如果时间超过10秒,则删除数据
var name = localStorage.getItem('name');
var name_time = localStorage.getItem('name_time');
if (Date.now() - name_time > 10000) {
  localStorage.removeItem('name');
  localStorage.removeItem('name_time');
}
Copy after login

Above The code will store the current timestamp when storing the data, and check the storage expiration time when reading the data. If it exceeds 10 seconds, the data will be deleted.

To summarize, the above is how JavaScript implements local storage. We can use localStorage to store data, read data, delete data, clear data and other common operations. At the same time, we can also write code to implement advanced functions such as setting data expiration time. Using localStorage can reduce server load and improve page speed and user experience.

The above is the detailed content of How to implement local storage in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!