In H4, cookies can be used to save user identity information in the client, but there are the following problems in permanent storage:

  • The size is limited to about 4KB
  • Bandwidth: Cookies are sent with HTTP transactions and take up a portion of the bandwidth
  • Inconvenient operation: Cookies do not have a native API to support operation

So sessionStorage and localStorage appear in H5.

1. The use of cookies

  • throughdocument.cookieGet all cookies
  • I’m going to add a cookie,document.cookie = newCookie, includingnewCookieUse key-value pairs for Settings such asname=titong, separated by semicolons (;).
  • Lookup requires a regular match or alignment of the cookies string to find
  • Delete directly specify the corresponding expiration timeexpireCan be

2.sessionStorage

  • Only valid for the current window, temporary save
  • Setting up new storage data is only requiredsetItem(key, value), key is the key name, value is the value to be accessed.
  • Read data usinggetItem(key)
  • Delete data usageremoveItem(key)

3.localStorage

  • The data is stored on the client’s local hardware device and remains valid even when the browser is closed.
  • Operation method andsessionStorageIs the same.