This time I will show you how to use localStorage and sessionStorage, how to use localStorage and sessionStorage? What are the precautions when using localStorage and sessionStorage? The following is a practical case, let’s take a look.
1. What is localStorage and sessionStorage
In HTML5, a new localStorage feature is added. This feature is mainly used as local storage and solves the problemcookieThe problem of insufficient storage space (the storage space of each cookie in the cookie is 4k). Generally, browsers support a size of 5M in localStorage. This will be different in localStorage in different browsers.
2. Advantages and limitations of localStorage
Advantages of localStorage
1. localStorage expands the 4K limit of cookies
2.localStorage can store the second The data of a request is directly stored locally. This is equivalent to a 5M database for the front-end page. Compared with cookies, it can save bandwidth, but this is only supported in higher versions of browsers
Limitations of localStorage
1. Browsers are not uniform in size, and only IE versions above IE8 support the attribute of localStorage
2. Currently all Browsers will limit the value type of localStorage to string type, which requires some conversion for our daily common JSON object types
3. localStorage is not readable in the privacy mode of the browser
4. LocalStorage essentially reads strings. If there is a lot of stored content, it will consume memory space and cause the page to become stuck.
5.localStorage cannot be The crawler captured
The only difference between localStorage and sessionStorage is that localStorage belongs to permanent storage, while sessionStorage belongs to When the session ends, the key-value pairs in sessionStorage will be cleared
Here we Use localStorage to analyze
3. The use of localStorage
Browser support for localStorage:
A special statement should be made here. If If you are using IE browser, then UserData will be used as storage. The main explanation here is the content of localStorage, so userData will not be explained too much, and in the blogger's personal opinion, it is not necessary. I came here to learn the use of UserData, because the current IE6/IE7 is in the phase of elimination, and many page development today will involve emerging technologies such as HTML5\CSS3, so we generally will not use it. Compatible
First of all, when using localStorage, we need to determine whether the browser supports the localStorage attribute
if(!window.localStorage){ alert("浏览器支持localstorage"); return false; }else{ //主逻辑业务
localStorage的写入,localStorage的写入有三种方法,这里就一一介绍一下 if(!window.localStorage){ alert("浏览器支持localstorage"); return false; }else{ var storage=window.localStorage; //写入a字段 storage["a"]=1; //写入b字段 storage.a=1; //写入c字段 storage.setItem("c",3); console.log(typeof storage["a"]); console.log(typeof storage["b"]); console.log(typeof storage["c"]); }
I don’t know if readers have noticed that the int type was just stored, but the print It comes out as string type. This is related to the characteristics of localStorage itself. LocalStorage only supports string type storage.
Reading of localStorage
if(!window.localStorage){ alert("浏览器支持localstorage"); }else{ var storage=window.localStorage; //写入a字段 storage["a"]=1; //写入b字段 storage.a=1; //写入c字段 storage.setItem("c",3); console.log(typeof storage["a"]); console.log(typeof storage["b"]); console.log(typeof storage["c"]); //第一种方法读取 var a=storage.a; console.log(a); //第二种方法读取 var b=storage["b"]; console.log(b); //第三种方法读取 var c=storage.getItem("c"); console.log(c); }
There are three ways to read localStorage. Among them, the official recommendation is getItem\setItem. These two methods are used to access it. Don’t ask me this. Why, because I don’t know this
I said before that localStorage is equivalent to a front-end database. The database mainly consists of four steps of adding, deleting, checking and modifying. The reading and writing here are equivalent to The two steps of adding and checking
Let's talk about the two steps of deleting and modifying localStorage
It is easier to understand this step. The idea is the same as that of re-changing global variables. The values are the same, here we will take an example to briefly explain
if(!window.localStorage){ alert("浏览器支持localstorage"); }else{ var storage=window.localStorage; //写入a字段 storage["a"]=1; //写入b字段 storage.b=1; //写入c字段 storage.setItem("c",3); console.log(storage.a); // console.log(typeof storage["a"]); // console.log(typeof storage["b"]); // console.log(typeof storage["c"]); /*分割线*/ storage.a=4; console.log(storage.a); }
I believe you have mastered the method after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
How to operate indexedDB in html5
##What are the new interactive features between H5 and C3
About how to handle old versions of browsers that are not compatible with H5 and C3
The above is the detailed content of How to use localStorage and sessionStorage. For more information, please follow other related articles on the PHP Chinese website!

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.