Web storage, a better local storage method than cookies
localStorage and sessionStorage
localStorage-data storage without time limit
sessionStorage-data storage for a session
//是否支持if(typeof(Storage)!=="undefined") {// 是的! 支持 localStorage sessionStorage 对象!// 一些代码.....} else {// 抱歉! 不支持 web 存储。}
localStorage object
There is no time limit for the data stored in the localStorage object
localStorage.sitename="小南瓜"; document.getElementById("result").innerHTML="网站名:" + localStorage.sitename;
Whether it is localStorage or sessionStorage, the APIs that can be used are the same, and the commonly used ones are as follows (localStorage For example):
保存数据:localStorage.setItem(key,value); 读取数据:localStorage.getItem(key); 删除单个数据:localStorage.removeItem(key); 删除所有数据:localStorage.clear(); 得到某个索引的key:localStorage.key(index);
Tips: Key/value pairs are usually stored as strings, and you can convert this format according to your needs.
if(typeof(Storage)!=="undefined") { if (localStorage.clickcount) { localStorage.clickcount=Number(localStorage.clickcount)+1; } else { localStorage.clickcount=1; } document.getElementById("result").innerHTML=" 你已经点击了按钮 " + localStorage.clickcount + " 次 "; } else { document.getElementById("result").innerHTML="对不起,您的浏览器不支持 web 存储。"; }
sessionStorage object
sessionStorage stores data for a session. The data will be deleted when the user closes the browser window
if(typeof(Storage)!=="undefined") { if (sessionStorage.clickcount) { sessionStorage.clickcount=Number(sessionStorage.clickcount)+1; } else { sessionStorage.clickcount=1; } document.getElementById("result").innerHTML="在这个会话中你已经点击了该按钮 " + sessionStorage.clickcount + " 次 "; } else { document.getElementById("result").innerHTML="抱歉,您的浏览器不支持 web 存储"; }
Simple website list program
<div> <label>网站名(key):</label> <input> <br> <label>网 址(value):</label> <input> <br> <input> <hr> <label>输入网站名:</label> <input> <input> <p><br></p> </div> <br> <div> </div> <script>// 载入所有存储在localStorage的数据 loadAll(); //保存数据 function save(){ var siteurl = document.getElementById("siteurl").value; var sitename = document.getElementById("sitename").value; localStorage.setItem(sitename, siteurl); alert("添加成功"); }//查找数据 function find(){ var search_site = document.getElementById("search_site").value; var sitename = localStorage.getItem(search_site); var find_result = document.getElementById("find_result"); find_result.innerHTML = search_site + "的网址是:" + sitename; }//将所有存储在localStorage中的对象提取出来,并展现到界面上function loadAll(){ var list = document.getElementById("list"); if(localStorage.length>0){ var result = "<table border='1'>"; result += "<tr><td>网站名<td>网址"; for(var i=0;i<localStorage.length;i++){ var sitename = localStorage.key(i); var siteurl = localStorage.getItem(sitename); result += "<tr><td>"+sitename+"<td>"+siteurl+""; } result += ""; list.innerHTML = result; }else{ list.innerHTML = "数据为空……"; } } </script>
Run Result:
JSON.stringify
## Store object data and convert the object to a string
var site = new Object; ...var str = JSON.stringify(site); // 将对象转换为字符串
JSON.parse
Convert the string into a JSON object<div> <label>别名(key):</label> <input> <br> <label>网站名:</label> <input> <br> <label>网 址:</label> <input> <br> <input> <hr> <label>输入别名(key):</label> <input> <input> <p><br></p> </div> <br> <div> </div> <script>//保存数据 function save(){ var site = new Object; site.keyname = document.getElementById("keyname").value; site.sitename = document.getElementById("sitename").value; site.siteurl = document.getElementById("siteurl").value;var str = JSON.stringify(site); // 将对象转换为字符串 localStorage.setItem(site.keyname,str); alert("保存成功"); } //查找数据 function find(){ var search_site = document.getElementById("search_site").value; var str = localStorage.getItem(search_site); var find_result = document.getElementById("find_result");var site = JSON.parse(str); find_result.innerHTML = search_site + "的网站名是:" + site.sitename + ",网址是:" + site.siteurl; } //将所有存储在localStorage中的对象提取出来,并展现到界面上// 确保存储的 keyname 对应的值为转换对象,否则JSON.parse会报错function loadAll(){ var list = document.getElementById("list"); if(localStorage.length>0){ var result = "<table border='1'>"; result += "<tr><td>别名<td>网站名<td>网址"; for(var i=0;i<localStorage.length;i++){ var keyname = localStorage.key(i); var str = localStorage.getItem(keyname); var site = JSON.parse(str); result += "<tr><td>"+site.keyname+"<td>"+site.sitename+"<td>"+site.siteurl+""; } result += ""; list.innerHTML = result; }else{ list.innerHTML = "数据为空..."; } } </script>The above is JSON. The result of stringify conversionThe following is the result of JSON.parse conversion
The above is the detailed content of How to use HTML5-Web storage?. For more information, please follow other related articles on the PHP Chinese website!

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.

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.

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.

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

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 (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 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.

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.


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

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

Atom editor mac version download
The most popular open source editor
