Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Web Storage in HTML5 local storage

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use Web Storage in HTML5 local storage. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Web Storage is a very important function introduced by HTML5, which can store data locally on the client side, similar to HTML4's cookie, but can be implemented much more powerful than cookie. The size of cookie is limited to 4KB Magi Web Storage official recommendation for each website 5MB.

There are two types of Web Storage:

SessionStorage

LocalStorage

It is clear from the literal meaning that sessionStorage saves the data in session and the browser is closed, while localStorage keeps the data locally on the client side.

The same API can be used for both sessionStorage and localStorage. The following are commonly used (take localStorage as an example):

Save data: localStorage.setItem (key,value)

Read data: localStorage.getItem (key)

Delete individual data: localStorage.removeItem (key)

Delete all data: localStorage.clear ()

Get the key:localStorage.key (index) of an index

As mentioned above, both key and value must be strings, in other words, the API of web Storage can only manipulate strings.

Next, we develop a simple address book Mini Program through Web Storage to demonstrate the use of related API; we want to implement the following functions:

Enter the contact. The contact has two fields: name and mobile phone number, and the mobile phone number is stored in localStorage as key.

Find the owner according to the mobile phone number.

List all currently saved contact information

First, prepare a simple HTML page, as follows:

The code is as follows:

Web Storage articles for HTML5 local storage

Name:

Mobile phone:

The interface is shown as follows:

To save contacts, simply implement the following JS method:

The code is as follows:

/ / Save data

Function save () {

Var mobilephone = document.getElementById ("mobilephone") .value

Var user_name = document.getElementById ("user_name") .value

LocalStorage.setItem (mobilephone,user_name)

}

To find the host, implement the following JS method:

The code is as follows:

/ / find data

Function find () {

Var search_phone = document.getElementById ("search_phone") .value

Var name = localStorage.getItem (search_phone)

Var find_result = document.getElementById ("find_result")

The owner of find_ result [XSS _ clean] = search_phone + "is:" + name

}

To display all saved contact information, you need to use the localStorage.key (index) method, as follows:

The code is as follows:

/ / extract all objects stored in localStorage and display them on the interface

Function loadAll () {

Var list = document.getElementById ("list")

If (localStorage.length > 0) {

Var result = ""

Result + = "name, mobile phone number"

For (var ionome0nteri0) {

Var result = ""

Result + = "name mobile phone company"

For (var iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report