Talk about HTML5 local storage technology_html5 tutorial skills
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:
- if(window.localStorage){
- alert('This browser supports localStorage');
- }else{
- 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:
- 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:
- var storage = window.localStorage;
- storage.name = “Tom”;
- //Get the name data
- var name1 = storage.getItem(“name”);
- 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:
- 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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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

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

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

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

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

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