How to read cookies in JavaScript

read cookies

The content of the cookie can be read directly through document.cookie:

  1. var strCookie = document . cookie ; 

At this point, strCookie is a string consisting of the name/value pairs of all cookies under the domain name, and the name/value pairs are separated by “semicolon and space”. For easy viewing, you can use the split() method to parse out the name/value pairs in the cookie to get a list of cookies. Then, use the corresponding decoding method to restore the value of the cookie.

The decoding method of the cookie value depends on the encoding method used when the cookie was previously stored. For example, use the encodeComponent() function to encode the value, then use the decodeComponent() function to decode the value. code show as below:

  1. function getCookie ( name ) {
  2. var cookies = document . cookie ;
  3. var list = cookies . split ( "; " );     // 解析出名/值对列表
  5. for ( var i

The post JavaScript Ways to Read Cookies first appeared on Lenix Blog .

This article is reprinted from
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.