Home Web Front-end H5 Tutorial Talk about HTML5 local storage technology_html5 tutorial skills

Talk about HTML5 local storage technology_html5 tutorial skills

May 16, 2016 pm 03:45 PM
html5 local storage

Local caching is a new technology that emerged in HTML5. The emergence of this technology makes the development of mobile web possible. We all know that when it comes to building a high-performance mobile app, speed is key. Before HTML5, only cookies could store data, and their size was only 4kb. This severely limits the storage of application files, resulting in long loading times for web-developed mobile applications. With local storage, web mobile applications can be closer to native.

In the browser, local storage is called through window.localStorage. The code to determine whether the browser supports local storage is as follows:

XML/HTML CodeCopy content to clipboard
  1. if(window.localStorage){
  2. alert('This browser supports localStorage');
  3. }else{
  4. alert('This browser does NOT supportlocalStorage');

If we want to store data locally, the easiest way is to window. Add a property to localStorage and assign a value to it. For example, if we want to store a data name whose value is Tom, we can achieve it in the following way:

XML/HTML CodeCopy content to clipboard
  1. window.localStorage.name = “Tom”

Note here that string variables require quotes. When we want to retrieve the data from local storage, we can use the getItem method. The entire code process is as follows:

JavaScript CodeCopy content to clipboard
  1. var storage = window.localStorage;
  2. storage.name = “Tom”;
  3. //Get the name data
  4. var name1 = storage.getItem(“name”);
  5. alert(name1);

The display result of this code in the Chrome browser console is a prompt box showing Tom. It can be seen that we have correctly stored and read data in this way.

If we want to delete these stored data, we can use the removeItem method. Add the following code to the above code:

JavaScript CodeCopy content to clipboard
  1. storage.removeItem(“name”);

At this time, when we alert again, null will be displayed because the data has been cleared.

After understanding some basic local storage usage and ideas, let’s systematically introduce local storage.

Local storage is divided into three major categories: localStorage/sessionStorage/local database

The usage, functions, calling methods, etc. of localStorage and sessionStorage are the same. They only have different meanings. Among them, the data stored in localStorage is valid for a long time, while the information stored in sessionStorage will be destroyed when each session is closed (in layman's terms, the data is automatically destroyed after the page is closed).

Due to the different characteristics of the two, the application scenarios are also very different. Usually, when we need to store some user configuration items and other data information that need to be stored for a long time, we need to use localStorgae to save it, taking advantage of its long-lasting characteristics. Correspondingly, when we need to implement session-based functions such as shopping carts, we need to use sessionStorage.

Since the usage of localStorage and sessionStorage are the same, we take localStorage as an example to introduce the methods of the two.

1. Set data setItem
The usage is localStorage.setItem("key", "value"), which means passing the value to key. (The usage method of sessionStorage.setItem is the same and will not be introduced one by one below)

2. Get data getItem
The usage is localStorage.getItem("key"). As long as you enter the corresponding key value, you can get the corresponding value value from it.

3. Delete specific data removeItem
The usage is localStorage.removeItem(key) to delete the data corresponding to the key.

4. Clear all data. The usage of clear
is localStorage.clear(), which means clearing all data in the storage system.

The above are some of the most basic sessionStorage/localStorage usage, I hope it will be helpful to everyone's learning.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles