The installation

bower

$ bower install storejs
Copy the code

npm

$ npm install storejs
Copy the code

Local storage APIs

store(key, data); // Store ({key: data, key2: data2}); // Store multiple string data in batches. // Get key string data store(); // Get all keys /data //store(false); (deprecated) 🔫 // Easy to empty library store(key,false) because of null values or errors; Store. Set (key, data[, overwrite]); //=== store(key, data); store.setAll(data[, overwrite]); //=== store({key: data, key2: data}); store.get(key[, alt]); //=== store(key); store.getAll(); //=== store(); store.remove(key); //===store(key,false) store.clear(); // Empty all keys /data store.keys(); // Return an array of all keys store.foreach (callback); // loop over, return false to end the loop. Has (key); Store ("test",function(arr){console.log(arr) {string (string){string (string) [3,4,5]}) store(["key","key2"],function(arr){ Console. log("arr:",arr) return "Change data one by one"})Copy the code

set

Set (key, data[, overwrite]); Store (key, data);

Store. The set (" WCJ ", "1") / / ⇒ 1 store. Set (" WCJ ")/delete / ⇒ WCJ and string dataCopy the code

setAll

Store. SetAll (data[, overwrite]) has the same effect store({key: data, key2: data});

store.setAll({ "wcj1":123, "Wcj2" : 345}) / / storage two string data store setAll ([" w1 ", "w2", "w3"]) / / store three string data / / 0 ⇒ ⇒ "w1" / / 1 "w2" / / 2 ⇒ "w3"Copy the code

get

Get (key[, Alt]) store(key)

Store ("wcj1") // Get the string data of wcj1. Store ("wcj1") // Same as aboveCopy the code

getAll

GetAll keys /data store.getall ()

Store. GetAll (Copy the code

clear

Clear all keys /data store.clear()

Discard store(false) because it is easy to empty the library by passing an empty value or reporting an error

keys

Returns an array of all keys store.keys()

Store the keys () / / ⇒ [" w1 ", "w2", "w3"]Copy the code

has

Returns true/false store.has(key)

remove

Delete key string data store. Remove (key)

store.remove("w1"); // Delete w1 return value store of w1 ("w1",false) // Delete w1Copy the code

forEach

Loop over, return false to end the loop

store.forEach(function(k,d){
    console.log(k,d)
    if (k== 3) return false
})Copy the code

Time to clear

(Make a note, the future will be time to clear the package, have ideas)

if (+new Date() > +new Date(2014, 11, 30)) { localStorage.removeItem("c"); // clear the c value // or localstorage.clear (); }Copy the code

Storage events

HTML5’s local storage also provides a storage event that can listen for changes in key/value pairs, using the following methods:

if(window.addEventListener){ window.addEventListener("storage",handle_storage,false); }else if(window.attachEvent){ window.attachEvent("onstorage",handle_storage); } function handle_storage(e){ if(! e){e=window.event; } //showStorage(); }Copy the code

For event variable E, is a StorageEvent object, which provides some useful properties to observe key-value pair changes, as shown in the following table:

Property Type Description
key String The named key that was added, removed, or moddified
oldValue Any The previous value(now overwritten), or null if a new item was added
newValue Any The new value, or null if an item was added
url/uri String The page that called the method that triggered this change

Compatible with

Source: sessionStorage localStorage

features Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit) iPhone(IOS) Android Opera Mobile Window Phone
localStorage 4 + 3.5 + 8 + 10.50 + 4 + 3.2 + 2.1 + 11 + 8 +
sessionStorage 5 + 2 + 8 + 10.50 + 4 + 3.2 + 2.1 + 11 + 8 +

Local storage size

Json.stringify (localStorage). Length Specifies the current capacity

Check the upper limit of localStore capacity