Respective advantages and scene of several storage methods
-
storage特点(sessionStorage,localStorage):
-
- The storage capacity of 5M
- The client is complete, the server will not process requests
-
sessionStorage data is not shared, localStorage share,
- api same
-
getItem // fetch record
-
setIten // Set Record
-
removeItem // remove records
-
// key takes a value corresponding to the key
-
clear // Clear History
-
- Memory contents
- Long as it is a sequence of the contents may be stored into a string
sessionStorage:
-
-
Act on the current page, a new page to open or close the page when the data disappeared;
- Different browser window data is not shared
-
localStorage:
-
- There is always, unless manually deleted,
- 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:
-
characteristic
- 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
- cookie can implement cross-page global variables
-
Common applications:
-
Save the user login status;
-
Track user behavior;
-
Custom page;
-
Create a shopping cart and so on ...
-
-
Shortcoming
- cookie may be disabled;
- cookie associated with the browser, you can not visit each other;
- the user cookie may be deleted;
- cookie security is not high enough;
- cookie storage space is very small (only about 4-10KB)