What are cookies?
A cookie is a variable stored in the visitor's computer. This cookie is sent whenever the same computer requests a page through the browser. You can use JavaScript to create and retrieve the value of a cookie.
Set cookies
Each cookie is a name/value pair, and you can assign a string like this to:
="userId=828";
It looks like a property that can assign different values. But it is different from general properties. Changing its assignment does not mean losing the original value. For example, the following two statements are executed continuously:
="userId=828"; ="userName=hulk";
At this time, the browser will maintain two cookies, namely userId and userName, so assigning values is more like executing statements like this:
("userId=828"); ("userName=hulk");
In fact, the browser sets the cookie in this way. If you want to change the value of a cookie, you just need to reassign the value, for example:
="userId=929";
This sets the cookie value named userId to 929.
Get the value of the cookie
The following describes how to get the value of a cookie. The value of the cookie can be obtained directly from:
var strCookie=;
This will get a string of multiple name/value pairs separated by semicolons that include all cookies under the domain name. For example:
="userId=828"; ="userName=hulk"; var strCookie=; (strCookie); //userId=828; userName=hulk
Of course, this needs to be run in the environment because it is to obtain the cookies under the current domain name.
It can be seen that you can only get all cookie values at once, but you cannot specify the cookie name to get the specified value. This is the most troublesome part of handling cookie values.
The user must analyze this string by himself to obtain the specified cookie value. For example, to obtain the userId value, you can do it like this:
="userId=828"; ="userName=hulk"; var strCookie=; (strCookie); //userId=828; userName=hulk function getdescookie(strcookie,matchcookie){ var getMatchCookie; var arrCookie=(";"); for(var i=0;i<;i++){ var arr=arrCookie[i].split("="); if(matchcookie == arr[0]){ getMatchCookie = arr[1]; break; } } return getMatchCookie; } var resultCookie = getdescookie(strCookie,'userId'); (resultCookie); //828
This gives the value of a single cookie.
If a cookie is created on a page, other pages in the directory where the page is located can also be accessed
This cookie. If there are subdirectories in this directory, they can also be accessed in the subdirectories.
For example, the cookies created in /html/ can be accessed by /html/ or /html/some/, but cannot be accessed by /.
In order to control the directory that cookies can access, you need to use the path parameter to set the cookie, and the syntax is as follows:
="name=value; path=cookieDir";
Where cookieDir means the directory where cookies can be accessed.
For example:
="userId=320; path=/shop";
This means that the current cookie can only be used in the shop directory.
If you want to make cookies available under the entire website, you can specify cookie_dir as the root directory, for example:
="userId=320; path=/";
Commonly used cookie operations and their function implementations
addCookie
addCookie(name,value,expireHours) This function receives 3 parameters: the cookie name, the cookie value, and how many hours it expires.
function addCookie(name,value,expireHours){ var exdate = new Date(); (() + expireHours * 60 * 60 * 1000); = c_name + "=" + escape(value) + ((expireHours == null) ? "" : ";expires=" + ()); }
getCookie
Get the cookie value of the specified name: getCookie(name)
This function returns a cookie value with the name name, and if it does not exist, it returns empty, and its implementation is as follows:
function getCookie(name){ var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = (reg)){ return (arr[2]); }else{ return null; } }
Or do not use regular matching, as shown in the following code:
function getCookie(name){ var strCookie=; var arrCookie=(";"); for(var i=0;i<;i++){ var arr=arrCookie[i].split("="); if(arr[0]==name){ return arr[1]; } } return null; }
deleteCookie
Delete the cookie with the specified name: deleteCookie(name)
This function can delete cookies with the specified name, and its implementation is as follows:
function deleteCookie(name){ var exp = new Date(); (() - 1); = name + "=v;expires=" + (); }
Here I used toUTCString() method, and I saw some of the toGMTString() used on the Internet in the formatting time.But this method is not favored. Please use toUTCString() to replace it! !See this sentence in W3C.
The above article is based on a comprehensive analysis in js. This is all the content I share with you. I hope you can give you a reference and I hope you can support me more.