COOKIE

  • cookieIs a location where data is stored as a string
  • Each HTTP request carries a cookie to the server in the request header
  • Each HTTP response carries a cookie to the client in the response header
  • In other words, cookies are data that automatically moves between client and server without our manual setting
  • We just need to set the contents of the cookie

The form in which cookies are stored

  • A cookie is stored as a string in which the cookie is stored as a stringkey=valueIn the form of
  • eachkey=valueIt’s a piece of data
  • Between multiple data;segmentation
// Cookie form 'a=100; b=200; c=300; 'Copy the code

The characteristics of the COOKIE

1, the storage size is limited, generally about 4 KB

2, the number is limited, generally about 50

3, there is a time, that is, there is an expiration time, generally at the session level (that is, the browser closed expiration)

4, there are domain name restrictions, that is, who set who can read

use

  • Use to read the contents of cookiesdocument.cookie
const cookie = document.cookie
console.log(cookie) // Get the current cookie value
Copy the code
  • Set the content of the cookie to usedocument.cookie
// Set a cookie document.cookie = 'a=100' // set a cookie document.cookie = 'b=200; expires=Thu, 18 Dec 2043 12:00:00 GMT"; '// The cookie data above will expire after 12 o 'clock on December 18, 2043 and will automatically disappear after the expiration dateCopy the code
  • Delete the content of the cookie useddocument.cookie
Cookie document.cookie = 'b=200; expires=Thu, 18 Dec 2018 12:00:00 GMT"; 'Copy the code

COOKIE operation encapsulation

  • Because there is no special operation COOKIE add, delete, change and check method in JS
  • So we need to encapsulate a method ourselves

Set the cookie

/** * setCookie sets the cookie *@param {STRING} Key Specifies the cookie name *@param {STRING} Value Cookie content to be set *@param {NUMBER} Expires Expires time */
function setCookie (key, value, expires) {
  const time = new Date()
  time.setTime(time.getTime() - 1000 * 60 * 60 * 24 * 8 + expires) // Used to set the expiration timedocument.cookie = `${key}=${value}; expires=${time}; `
}
Copy the code

Read the cookie

/** * getCookie Gets an attribute * in the cookie@param {STRING} Key The cookie property you want to query@return {STRING} The value of the cookie property you want to query is */
function getCookie(key) {
  const cookieArr = document.cookie.split('; ')
​
  let value = ' '
​
  cookieArr.forEach(item= > {
    if (item.split('=') [0] === key) {
      value = item.split('=') [1]}})return value
}
Copy the code

Delete the cookie

/** * delCookie Deletes an attribute * in the cookie@param {STRING} Name The name of a cookie property you want to delete */
function delCookie(name) {
  setCookie(name, 1, -1)}Copy the code

More recommended

  • JavaScript Learning Notes (25) – HTTP
  • MYSQL > MYSQL > MYSQL
  • JavaScript Learning Notes (23) – Server PHP
  • JavaScript Learning Notes (22) — Prototypes and prototype chains