h5-- local storage sessionStorage, localstorage

Respective advantages and scene of several storage methods

  • storage特点(sessionStorage,localStorage):

    1. The storage capacity of 5M
    2. The client is complete, the server will not process requests
    3. sessionStorage data is not shared, localStorage share,
    4. api same
      1. getItem // fetch record

      2. setIten // Set Record

      3. removeItem // remove records

      4. // key takes a value corresponding to the key

      5. clear // Clear History

    5. Memory contents
      1. Long as it is a sequence of the contents may be stored into a string

sessionStorage:

    1. Act on the current page, a new page to open or close the page when the data disappeared;
    2. Different browser window data is not shared

localStorage:

    1. There is always, unless manually deleted,
    2. Different browser data sharing
If data have modified or deleted, it will trigger an event storage
 
    On the window object data change is not triggered
Note: session with the window can, example: iframe operation
   window.addEventListener ( 'Storage', function (E) { 
            the console.log (e.key) // modify or delete the key value 
            console.log (e.newValue) // value newly stored 
            console.log (e.oldValue ) // call the value before the change 
            console.log (e.storageArea) // current storage objects 
            console.log (e.url) // url of the document that triggered the change of script 
         }); 
  • cookie

    1. characteristic

      1. cookie can implement cross-page global variables 
        cookie can span multiple pages under the same domain name, but can not span multiple domains make! use 
        the same website all the pages share a cookie 
        you can set expiration date 
        storage space around 4-10KB
    2. Common applications:

      1. Save the user login status;

      2. Track user behavior;

      3. Custom page;

      4. Create a shopping cart and so on ... 

    3. Shortcoming

      1. cookie may be disabled;
      2. cookie associated with the browser, you can not visit each other;
      3. the user cookie may be deleted;
      4. cookie security is not high enough; 
      5. cookie storage space is very small (only about 4-10KB)

 

Guess you like

Origin www.cnblogs.com/mengtong/p/10991675.html