1. Overview
Web Storage is a method of storing data on the client side. Compared with Cookie, Web Storage is more secure and can store more objects, not just strings; Web Storage can store Larger data, rather than cookies that can only store a few KB of data; Web Storage can also reduce the conversion of data between the client and the server, reducing bandwidth.
The core of Web Storage is the two sub-objects of the window object, sessionStorage and localStorage. Data is stored in the form of key-value pairs through these two objects. Both objects implement storageinterface, have properties and methods with the same names, and have the same usage. The difference is the storage time and sharing scope of the two.
The storage time of sessionStorage data depends on the existence time of the browser window or tab where it is stored, and the sharing scope is only within the browser window or tab where it was generated.
The storage time of localStorage data exceeds the opening time of the browser. Unless deleted manually or through code, its sharing scope is between pages of the same origin website.
Many browsers do not support sessionStorage access to the local file system , so it must be used through the web server.
The specifications of Web Storage allow the storage of any type of data, but most browser implementations allow the storage of string text data, but you can use Web Storage combined with JSON technology to store non-text data.
A more advanced data storage method is Web SQL Database for database storage based on indexes and has the SQL query language SQLite. Web SQL Database is currently only supported by Safari, Chrome and Opera. The final specification will most likely not take this approach. Another method is IndexedDB, which is only supported by FireFox 4 or above.
2. Browser support detection
function checkStorageSupport() { //sessionStorage if (window.sessionStorage) { alert('This browser supports sessionStorage'); } else { alert('This browser does NOT support sessionStorage'); } //localStorage if (window.localStorage) { alert('This browser supports localStorage'); } else { alert('This browser does NOT support localStorage'); } }
3.Storage interface
interface Storage { //同源键值对的数目 readonly attribute unsigned long length; //通过索引获取键,索引从0开始 getter DOM String key (in unsigned long index); //通过键获取值,键若不存在,值将返回 null getter any getItem(in DOMString key); //存储键值对,若已存在同名键,则值将被覆盖。若用户关闭了浏览器存储或是已超 //过允许存储的最大值,都将产生QUOTA_EXCEEDED_ERR错误。 set ter creator void setItem(in DOMString key, in any data); //通过键删除值,不存在则什么也不做 delete r void removeItem(in DOMString key); //删除storage中所有键值对,若为空则什么也不做 void clear (); };
4. Reading and storing data
//---------------方式一-------------- //存储数据 window.sessionStorage.setItem(‘myFirstKey’, ‘myFirstValue’); //读取数据 alert(window.sessionStorage.getItem(‘myFirstKey’)); //---------------方式二-------------- //存储数据 window.sessionStorage.myFirstKey = ‘myFirstValue’; //读取数据 alert(window.sessionStorage.myFirstKey); //---------------方式三-------------- var varKey = sessionStorage.key(index); window.sessionStorage[varKey] = new Value;
5. StorageEvents
Communication between Web Storage and other pages, browser windows or tabs, and Web Workers can be carried out through storage events. Objects of the same origin can listen to storage events. Add the storage event listening method as follows:
window.addEventListener("storage", displayStorageEvent, true);
6.StorageEvent interface
The storage event object implements the StorageEvent interface. The declaration of this interface is as follows:
interface StorageEvent : Event { readonly attribute DOMString key; readonly attribute any oldValue; readonly attribute any newValue; readonly attribute DOMString url; //该方法提供了一个对发生storage事件对象的 引用 ,这个对象可以是 //sessionStorage或localStorage readonly attribute Storage storageArea; };
6. Handling the maximum quota
Most browsers allow Web Storage not to exceed 5MB. In order to prevent the quota limit from being exceeded when storing data, you can use the method of catching the QUOTA_EXCEEDED_ERR exception to handle it, for example:
try { sessionStorage["name"] = "Tabatha"; } catch ( exception ) { if (exception == QUOTA_EXCEEDED_ERR) { // we should tell the user their quota has been exceeded. } }
The above is the detailed content of A brief description of HTML5-Web Storage APIs. For more information, please follow other related articles on the PHP Chinese website!

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

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.


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

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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