search
HomeWeb Front-endH5 TutorialHTML5 local storage localStorage, sessionStorage basic usage, traversal operations, exception handling, etc._html5 tutorial skills

The usage methods of localStorage and sessionStorage in the local storage API of HTML5 are the same. The difference is that sessionStorage is cleared after closing the page, while localStorage will always be saved. Here we take localStorage as an example to briefly introduce the local storage of HTML5 and provide some examples for common problems such as traversal. localStorage is the HTML5 local storage API, which uses key-value pairs to access data. The accessed data can only be strings. Different browsers have different support for this API, such as usage methods, maximum storage space, etc.

1. Basic usage of localStorage API

The usage of localStorage API is simple and easy to understand. The following are common API operations and examples: Set data: localStorage.setItem(key,value); Example:

Copy code
The code is as follows:

for(var i=0; i localStorage.setItem(i,i) ;
}

Get data: localStorage.getItem(key) Get all data: localStorage.valueOf() Example:

Copy Code
The code is as follows:
for(var i=0; i localStorage.getItem(i);
}

Delete data: localStorage.removeItem(key) Example:

Copy code
The code is as follows:
for(var i=0; i localStorage.removeItem(i);
}

Clear all data: localStorage.clear() Get the number of local storage data :localStorage.length Get the key value of the Nth data: localStorage.key(N)

2. Traverse key key value method


Copy code
The code is as follows :

for(var i=localStorage.length - 1; i >=0; i--){
console.log('(i 1) 'th data The key value is: ' localStorage.key(i) ', the data is: ' localStorage.getItem(localStorage.key(i)));
}


3. Storage size limit testing and exception handling

3.1 Data storage size limit test

Different browsers basically have limitations on the local storage size of HTML5. The results of a test are as follows:

Copy code
The code is as follows:
IE 9 > 4999995 5 = 5000000
firefox 22.0 > 5242875 5 = 5242880
chrome 28.0 > 2621435 5 = 2621440
safari 5.1 > 2621435 5 = 2621440
opera 12.15 > 5M (If it exceeds, a dialog box allowing request for more space will pop up)


Test code reference:


Copy code
The code is as follows:




<script><br /> function log( msg ) {<br /> console.log(msg);<br /> alert(msg);<br /> }&lt ;/p> <p> var limit;<br /> var half = '1'; //This will be changed to Chinese and run again<br /> var str = half;<br /> var sstr;<br /> while ( 1 ) {<br /> try {<br /> localStorage.clear();<br /> str = half;<br /> localStorage.setItem( 'cache', str );<br /> half = str;<br /> } catch ( ex ) { <br /> break;<br /> }<br /> }<br /> var base = str.length;<br /> var off = base / 2;<br /> var isLeft = 1;<br /> while ( off ) {<br /> if ( isLeft ) {<br /> end = base - (off / 2);<br /> } else {<br /> end = base (off / 2);<br /> } <p> sstr = str.slice( 0, end );<br /> localStorage.clear();<br /> try {<br /> localStorage.setItem( 'cache', sstr );<br /> limit = sstr. length;<br /> isLeft = 0;<br /> } catch ( e ) {<br /> isLeft = 1;<br /> } <p> base = end;<br /> off = Math.floor( off / 2 );<br /> } <p> log( 'limit: ' limit );<br /></script>


3.2 Data storage exception handling


Copy code
The code is as follows:

try{
localStorage.setItem( key, value);
}catch(oException){
if(oException.name == 'QuotaExceededError'){
console.log('Local storage limit exceeded!');
// If the historical information is no longer important, you can clear it and then set it again
localStorage.clear();
localStorage.setItem(key, value);
}
}
Statement
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
H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

H5 vs. HTML5: Clarifying the Terminology and RelationshipH5 vs. HTML5: Clarifying the Terminology and RelationshipMay 05, 2025 am 12:02 AM

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

HTML5 Features: The Core of H5HTML5 Features: The Core of H5May 04, 2025 am 12:05 AM

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

H5: Exploring the Latest Version of HTMLH5: Exploring the Latest Version of HTMLMay 03, 2025 am 12:14 AM

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Beyond Basics: Advanced Techniques in H5 CodeBeyond Basics: Advanced Techniques in H5 CodeMay 02, 2025 am 12:03 AM

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

H5: The Future of Web Content and DesignH5: The Future of Web Content and DesignMay 01, 2025 am 12:12 AM

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5: New Features and Capabilities for Web DevelopmentH5: New Features and Capabilities for Web DevelopmentApr 29, 2025 am 12:07 AM

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

H5: Key Improvements in HTML5H5: Key Improvements in HTML5Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software