Before operating the cookie, first look at the cookie look like.
Can be seen, a Cookie is a key (form "key = value") of a combination of space-separated with a semicolon, the form: "name1 = value1; name2 = value2; name3 = value3"
set cookie:
1 / ** 2 * Set Cookie . 3 * @param name cookie names of 4 * @param value cookie values of 5 * @param day cookie's expiration date . 6 * / . 7 the setCookie var = function (name, value, Day) { . 8 if (! day == 0) { // when the set time is equal to 0, expires property is not set, Cookie deleted when the browser is closed . 9 var expires = Day * 24 * 60 * 60 * 1000; 10 DATE = var new new DATE (+ new new DATE () + Expires); . 11 the document.cookie = name + "=" + Escape (value) + "; Expires =" + date.toUTCString (); 12 is the else {} 13 is the document.cookie = name + "=" Escape + (value); 14} 15};
Note: expires GMT or UTC time format, I do not have to specify the path (path) and domain (domain), the default is the current path when the path is not specified, such as for "https://home.cnblogs.com/u / maderlzp / "cookie set down, its path is" / u / maderlzp ", its domain for the current domain name" home.cnblogs.com ".
Why sometimes can not delete the cookie? May be because you do not specify the cookie path and domain When you delete a cookie, this cookie can not find cause to set the expiration time can not be deleted.
Get cookie:
/ ** * Get the name of a cookie corresponding to the name of * @param name cookie * @returns {null} does not exist, return null * / var the getCookie = function (name) { var ARR; var = new new REG the RegExp ( "(^ |) "+ name +" = ([^;] *) (; | $) "); IF (ARR = document.cookie.match (REG)) return unescape (ARR [2]); the else return null; } ;
cookie get regular analysis:
"(^ |)" + Name + "= ([^;] *) (; | $)" (^ |) ( the space between the cookie separated by a semicolon key-value pairs) to match the beginning of cookie or space, that is, key start to the cookie. Followed by the name of the cookie name, ([^;] * ) matches any character other than a semicolon, i.e. key-value pairs cookie. Finally (; | $) matches the end of a semicolon or a whole cookie, that is the end of cooke key-value pairs.
More regular syntax Detailed please refer to:
Usage of regular expressions and common examples [1]
Delete cookie:
1 / * 2 * delete the Cookie . 3 * @param name cookie's name . 4 * / . 5 delCookie var = function (name) { . 6 the setCookie (name, '', -1); . 7};
Set to delete the cookie expires is past time to
Modify cookie:
Using the above method to set a cookie, the cookie back to the assignment to be modified on the line, so the old will be overwritten
cookie's main role:
Cookie is mainly used in the following three aspects:
- Session state management (such as user login status information, shopping cart, game scores and other records required)
- Personalized settings (such as user-defined settings, themes, etc.)
- Browser behavior tracking (such as tracking user behavior analysis)
cookie settings Syntax:
document.cookie = "cookieName=mader; expires=Fri, 31 Dec 2017 15:59:59 GMT; path=/mydir; domain=cnblogs.com; max-age=3600; secure=true";
- cookieName = mader: name = value, cookie name and value
- expires = Fri, 31 Dec 2017 15:59:59 GMT: expires, cookie expiration date, if not defined, cookie will expire at the end of the conversation. The date format for the new Date (). ToUTCString ()
- path = / mydir: path = path (e.g. '/', '/ mydir'), if not defined, the default path for the current document position.
- domain = cnblogs.com: specified domain (e.g. 'example.com', '.example.com' (including all sub-domain), 'subdomain.example.com'), if not defined, the default is the domain name of the current path of the document is section.
- max-age = 3600: After the document is viewed cookie expiration time, in seconds.
- secure = true: cookie will only be transmitted https, that is encrypted https link transmission
Before operating the cookie, first look at the cookie look like.
Can be seen, a Cookie is a key (form "key = value") of a combination of space-separated with a semicolon, the form: "name1 = value1; name2 = value2; name3 = value3"
set cookie:
1 / ** 2 * Set Cookie . 3 * @param name cookie names of 4 * @param value cookie values of 5 * @param day cookie's expiration date . 6 * / . 7 the setCookie var = function (name, value, Day) { . 8 if (! day == 0) { // when the set time is equal to 0, expires property is not set, Cookie deleted when the browser is closed . 9 var expires = Day * 24 * 60 * 60 * 1000; 10 DATE = var new new DATE (+ new new DATE () + Expires); . 11 the document.cookie = name + "=" + Escape (value) + "; Expires =" + date.toUTCString (); 12 is the else {} 13 is the document.cookie = name + "=" Escape + (value); 14} 15};
Note: expires GMT or UTC time format, I do not have to specify the path (path) and domain (domain), the default is the current path when the path is not specified, such as for "https://home.cnblogs.com/u / maderlzp / "cookie set down, its path is" / u / maderlzp ", its domain for the current domain name" home.cnblogs.com ".
Why sometimes can not delete the cookie? May be because you do not specify the cookie path and domain When you delete a cookie, this cookie can not find cause to set the expiration time can not be deleted.
Get cookie:
/ ** * Get the name of a cookie corresponding to the name of * @param name cookie * @returns {null} does not exist, return null * / var the getCookie = function (name) { var ARR; var = new new REG the RegExp ( "(^ |) "+ name +" = ([^;] *) (; | $) "); IF (ARR = document.cookie.match (REG)) return unescape (ARR [2]); the else return null; } ;
cookie get regular analysis:
"(^ |)" + Name + "= ([^;] *) (; | $)" (^ |) ( the space between the cookie separated by a semicolon key-value pairs) to match the beginning of cookie or space, that is, key start to the cookie. Followed by the name of the cookie name, ([^;] * ) matches any character other than a semicolon, i.e. key-value pairs cookie. Finally (; | $) matches the end of a semicolon or a whole cookie, that is the end of cooke key-value pairs.
More regular syntax Detailed please refer to:
Usage of regular expressions and common examples [1]
Delete cookie:
1 / * 2 * delete the Cookie . 3 * @param name cookie's name . 4 * / . 5 delCookie var = function (name) { . 6 the setCookie (name, '', -1); . 7};
Set to delete the cookie expires is past time to
Modify cookie:
Using the above method to set a cookie, the cookie back to the assignment to be modified on the line, so the old will be overwritten
cookie's main role:
Cookie is mainly used in the following three aspects:
- Session state management (such as user login status information, shopping cart, game scores and other records required)
- Personalized settings (such as user-defined settings, themes, etc.)
- Browser behavior tracking (such as tracking user behavior analysis)
cookie settings Syntax:
document.cookie = "cookieName=mader; expires=Fri, 31 Dec 2017 15:59:59 GMT; path=/mydir; domain=cnblogs.com; max-age=3600; secure=true";
- cookieName = mader: name = value, cookie name and value
- expires = Fri, 31 Dec 2017 15:59:59 GMT: expires, cookie expiration date, if not defined, cookie will expire at the end of the conversation. The date format for the new Date (). ToUTCString ()
- path = / mydir: path = path (e.g. '/', '/ mydir'), if not defined, the default path for the current document position.
- domain = cnblogs.com: specified domain (e.g. 'example.com', '.example.com' (including all sub-domain), 'subdomain.example.com'), if not defined, the default is the domain name of the current path of the document is section.
- max-age = 3600: After the document is viewed cookie expiration time, in seconds.
- secure = true: cookie will only be transmitted https, that is encrypted https link transmission