HTML5 Localstorage Local Storage

Source: Internet
Author: User
Tags object implement key log return string window sessionstorage

Localstorage Local Storage Basic usage

How to create and access Localstorage above. We will do a refresh page, increase the number of visits to the function! Very simply, the code is as follows: above is a point of operation, Localstorage also has a storage API, specifically as follows: Call the SetItem () method, the corresponding name and value passed out, you can implement data storage call GetItem () method, the name passed out, You can get the corresponding value call the RemoveItem () method, the name as a parameter, you can delete the corresponding data call clear () method, you can delete all stored data using the Length property and the key () method, the number of incoming 0~length-1, You can enumerate the names of all the stored data usage is as follows:Localstorage.setitem ("name", "Haorooms");            Stores a string Localstorage.getitem ("name") with the name "name"; Gets the value//of ' name ' to enumerate all stored name/value pairs for (var i=0; i low version browser storage compatibility scheme   IE User Data   Microsoft has implemented its proprietary client storage mechanism-"userData" in IE5 and later IE browsers.   UserData can implement a certain amount of string data storage, which can be used as an alternative to Web storage for IE8 previous IE browsers.   Ilocalstorage plugin   because the local storage API for IE8 the following browsers is not the same, so write a plug-in compatible with the major browser storage. The methods and usage provided are as follows:   GetItem: Get Properties SetItem: Set properties RemoveItem: Delete attributes   Ilocalstorage.setitem (' key ', ' value '); Console.log (Ilocalstorage.getitem (' key ')); Ilocalstorage.removeitem (' key '); Plug-in code  /**  * localstorage Local storage compatible functions  * GetItem: Getting properties  * SetItem: Setting properties  * removeitem: Deleting properties  *  *  * @example  *     ilocalstorage.setitem (' key ', ' value ');     Console.log (Ilocalstorage.getitem (' key '));     Ilocalstorage.removeitem (' key ');  *  */    (function (window, document) {     //1. IE7 UserData Object     var UserData = {        Userdata:null,         NAM E:location.href,         init:function () {          //IE7 initialization             if (! Userdata.userdata) {                try{                    Userdata.userdata = document.createelement ("INPUT");                     UserData.userData.type = "hidden";                     UserData.userData.style.display = "none";                     UserData.userData.addBehavior ("#default #userdata") ;                     Document.body.appendChild (userdata.userdata);                     var expires = new Date ();                     expires.setdate (expires.getdate () + 365);                     UserData.userData.expires = expires.toutcstring ();                } catch (e) {              &NBSP ;     return false;                            }             return true;        },           setitem:function (key, value) {      &NBS P     if (Userdata.init ()) {                UserData.userData.load (userdat A.name);                 UserData.userData.setAttribute (key, value);                 UserData.userData.save (userdata.name);                    },           Getitem:fun Ction (key) {            if (Userdata.init ()) {                UserData.userData.load (userdata.name); nbsp               return UserData.userData.getAttribute (key);             {       },           RemoveItem: function (key) {            if (Userdata.init ()) {            &N Bsp   UserData.userData.load (userdata.name);                 UserData.userData.removeAttribute (key);                 UserData.userData.save (userdata.name);            }         {     };      //2. Compatible only Globalstorage browser    //use: var storage = getlocalstorage ();     Function Getlocalstorage () {        if typeof localstorage == "Object" {            return localstorage;        } else if (typeof G Lobalstorage = = "Object") {            return globalstorage[location.href];     &N Bsp  } else if (typeof UserData = = "Object") {            return Globalstorage[location.hre F];        } else{            throw new Error ("Do not support local storage");     &N Bsp  }    }     var storage = getlocalstorage ();       Function ilocalstorage () {     }      /Advanced Browser Localstorage object     Ilocalstorage.prototype = {        setitem:function (key, value) {            if (!window.localstorage) {                Userdata.setitem (Key, Valu e);            }else{               Storage.setitem (key, value);                    },           Getitem:fun Ction (key) {            if (!window.localstorage) {                return Userdata.getitem (key);            }else{                return storage.ge TItem (key);             {       },           RemoveItem: function (key) {            if (!window.localstorage) {          &NBS P     Userdata.removeitem (key);            }else{                Storage.removeite M (key);            }         &N  Bsp }         if (typeof module = = ' object ') {        module.exports = new Ilocalstor Age ();    }else {        window.ilocalstorage = new Ilocalstorage ();    }  }) (WI Ndow, document);   Other plugins

Localforage:https://github.com/mozilla/localforage

Store.js:https://github.com/marcuswestin/store.js



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.