How to use local storage for local storage in H5

How does localStorage are stored in the local storage value in H5? This issue may be often seen in our daily study or work. I hope that you can give you a deep harvest through this issue. Below is the reference content brought by Xiaobian, let’s take a look!

The biggest difference between the two storage technologies of H5 is the life cycle. 1. LocalStorage is a local storage, the storage period is not limited;

2. SessionStorage session storage, page closing data will be lost.

LocalStorage.SetItem (“Key”, “Value”) // Storage

LocalStorage.GetItem (key) // Value according to KEY

localStorage.Valueof () // Get all values ​​

localStorage.RemoveItem (“key”) // Delete a single value, pay attention to quotation marks

localStorage .clear () // Delete all data

LocalStorage.Length // Number of data

LocalStorage.Key (N) // Nth data is obtained

LocalStorage and sessionStorage are the same, using methods

Several summary:

localStorage.key = 1; // Set storage, named Key The value is 1

LocalStorage.RemoveItem (“key”); // Remove the storage key, remember that the KEY Equation No.

The following is the test of a practical example:

To implement the text content of the input, you have locally stored to achieve the browser, then turn it back,The content is still in (commonly used in the resumption of the resumption of the resumption of the phone nail log).

First, get a text field on the page

, below is jQuery:

IF (! LocalStorage.getItem (“text”)) / / WINDOW object, the front of Window omitted 省 LocalStorage.SetItem (“Text”, “); // First judge, do a blank storage, otherwise return null Show out, here’s IF braces save LocalStorage.Text = localStorage.getItem (“text”); // Value $ (“textarea”). HTML (LocalStorage.Text); // Displays $ (“Textarea”). Keyup () {/// There are a lot here, such as Blur, Change, KeyDown, and a timer, which is used, practical for multi-field storage .SetItem (“text”, $ (this) .val ()); // Re-store});
   The above can achieve a practical small function, reflecting the H5 local storage or useful, of course, the field is available, the JSON method is available! See here, it is loaded from the network 

{Alert (‘OK’);} else {alert (‘fail’);} // set value SessionStorage.SetItem (‘Key_a’, 1); // Value Var Key_a = sessionStorage.GetiTEM (‘key_a’); console.log (key_a); // Delete sessionStorage.RemoveItem (‘key_a’); console.log (sessionStorage.getItem (‘key_a’)); // null sessionStorage.SetItem (‘key_b’ , 1); sessionStorage.SetItem (‘key_c’, 2); // Clear all key value sessionStorage.clear (); console.log (sessionStorage.Key_B); console.log (sessionStorage.Key_C); console.log ====================); // Setting the value and the value can also be used. Symbol, similar to the object attribute // setting value sessionStorage.Key_D = 12; / / Take the value var key_d = sessionStorage.key_d; console.log (key_d); // has a small difference, if this key is gone. A return value undefined, one is null console.log (sessionStorage.key_null; // undefined console.log (sessionStorage.getitem (‘key_null’)); // null console.log (‘======== == Simple Demonstrate An example of a storage object ========); var obj = {A: 12, B: [1, 2, 3, 4, 5],C: {x: ‘a’, Y: [‘BB’, 12, ‘CC’, {A: 1, B: 2}], Z: 1333}}; sessionStorage.STITEM (‘Page’, JSON.STRINGYFY (OBJ)); // Value VAR Page = JSON.PARS (sessionStorage.getItem (‘Page’)); console.log (page); // Traversed the array for (VAR i = 0; i
   < page.b.length;i++){
    // 遍历对象,通常用in
    for(var j in page.c){
    // 删除key
 Example: Counter, refresh page, can be seen:  

VAR Storage = window.localStorage; If (! storage.getitem (“PageLoadCount”)) Storage.SetItem (“PageLoadCount”, 0); Storage.pageLoadCount = PARSEINT (“PageLoadCount”) + 1; // Must format to convert Document.getElementByid ( “TEST”). Innerhtml = storage.pageLoadCount; // ShowStorage ();

Need to note that HTML5 local storage can only save strings, any format stored It will be automatically converted to a string, so it is necessary to perform type conversion. This is the reason why PARSEINT must be used in the last code.
  Thank you for reading! Read the above Content, you have LO to H5How does CalStorage have a new value of the local storage brush? I hope that the content of the article will help everyone. If you want to know more about the content, please pay attention to Tumi Yun Industry Information Channel. 

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment