HTML5 Web Storage

Source: Internet
Author: User
Tags sessionstorage

HTML5 web storage, better than cookies. more secure and fasterdata store in name/value pairsthe storage limit is at least 5 MB 1. development Process: 2. browser support: 3. HTML5 Web Storage provides the following two types of objects: window. localStorage-stores data with no expiration date does not have an expiration date sessionStorage-stores data for one session (data is lost when the tab is closed). the following uses localStorage as An Example 4. basic usage: Determine whether the browser supports copying the Code if (typeof (Storage)! = "Undefined") {// Code for localStorage/sessionStorage.} else {// Sorry! No Web Storage support ..} use the copy code: copy the code localStorage. setItem ("name", "wish"); // storelocalStorage. name = "wish"; localStorage ["name"] = "wish"; localStorage. getItem ("name"); // retrieve localStorage. removeItem ("name"); // remove localStorage. clear (); // Delete All localStorage. key (I); // get key copy code Note: localStorage calltostring on all stored values. therefore, if it is a number or other types, it will also be converted to String 5. for example, in google resource, view: removeItem and modify the log to disappear. Simple page access count: Use localStorage: copy the Code if (localStorage. visitcount) {localStorage. visitcount = Number (localStorage. visitcount) + 1; // type to be converted} else {localStorage. visitcount = 1;} document. getElementById ("visitTimes "). innerHTML = "You have visited" + localStorage. visitcount + "time (s ). "; Use sessionStorage to copy the Code if (sessionStorage. visitcount) {sessionStorage. visitcount = Number (sessionStorage. visitcount) + 1; // type to be converted} else {sessionStorage. visitcount = 1;} document. getElementById ("visitTimes "). innerHTML = "You have visited" + sessionStorage. visitcount + "time (s ). "; copy Code 6. store on github. js is based on localStorage: store. js exposes a simple API for cross browser local storage no compatibility problem: store. js uses localStorage when available, and falls back on the userData behavior in IE6 and IE7 storage will not tostring: store. js uses JSON. stringify () and JSON. parse () on each call to store. set () and store. get () is basically used as follows: copy the code store. set ('name', 'wish ') // Store. get ('name') // Get store. remove ('username') // Remove 'name' store. clear () // Clear all keys

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.