What is a cookie?
A cookie is a variable stored on a visitor's computer. This cookie is sent whenever a page is requested by the same computer through a 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 such as the following to Document.cookie:
Document.cookie= "userid=828";
Document.cookie looks like an attribute that can be assigned a different value. However, unlike the general properties, changing its assignment does not mean losing the original value, such as executing the following two statements consecutively:
Document.cookie= "userid=828";d ocument.cookie
At this point the browser will maintain two cookies, namely UserID and username, so give Document.cookie a value like a statement like this:
Document.addcookie ("userid=828");d Ocument.addcookie ("Username=hulk");
In fact, the browser is in this way to set the cookie, if you want to change the value of a cookie, simply re-assign the value, for example:
Document.cookie= "userid=929";
This sets the cookie value named UserID to 929.
Get the value of a cookie
Here's how to get the value of a cookie. The value of a cookie can be obtained directly from Document.cookie:
Var
This will get a string of multiple name/value pairs separated by semicolons that includes all the cookies under that domain name. For example:
Document.cookie= "userid=828";d ocument.cookie= "Username=hulk"; var strcookie=//userid=828; Username=hulk
Of course this is going to run in the environment, because it is to get the cookie under the current domain name.
This shows that it is only possible to get all the cookie values at once, rather than specifying a cookie name to get the specified value, which is the most troublesome part of handling cookie values.
The user must parse the string himself to get the specified cookie value, for example, to get the value of the UserID, this can be done:
Document.cookie= "userid=828";d Ocument.cookie= "Username=hulk";varStrcookie=Document.cookie;console.log (Strcookie);//userid=828; Username=hulkfunctionGetdescookie (strcookie,matchcookie) {varGetmatchcookie; varArrcookie=strcookie.split (";"); for(vari=0;i<arrcookie.length;i++){ varArr=arrcookie[i].split ("="); if(Matchcookie = = Arr[0]) {Getmatchcookie= Arr[1]; Break; } } returnGetmatchcookie;}varResultcookie = Getdescookie (Strcookie, ' userId ')); Console.log (Resultcookie); //828
This gives you the value of a single cookie .
If a cookie is created on a page, other pages in the same directory as the page can also be accessed
The cookie. If there are subdirectories under this directory, they can also be accessed in subdirectories.
For example, a cookie created in www.xxxx.com/html/a.html can be www.xxxx.com/html/b.html or www.xxx.com/ html/some/c.html, but cannot be accessed by www.xxxx.com/d.html .
To control which directories the cookie can access, you need to use the path parameter to set the cookie with the following syntax:
Document.cookie= "Name=value; Path=cookiedir ";
Where Cookiedir represents the directory in which cookies can be accessed.
For example:
Document.cookie= "userid=320; Path=/shop ";
means that the current cookie can only be used in the shop directory.
If you want to make a cookie available under the entire Web site, you can specify Cookie_dir as the root directory, for example:
Document.cookie= "userid=320; path=/";
Common cookie operation and its function implementation Addcookie
Addcookie (name,value,expirehours) The function receives 3 parameters: The cookie name, the cookie value, and the number of hours after which it expires.
function Addcookie (name,value,expirehours) { varnew Date (); + expirehours * * * +); null) ? "": "; expires=" + exdate.toutcstring ());}
GetCookie
Gets the cookie value for the specified name: GetCookie (name)
The function returns a cookie value named name, or null if it does not exist, with the following implementation:
functionvarnew RegExp ("(^|)" + name + "= ([^;] *)(;|$)"); if (arr = Document.cookie.match (reg)) { return (arr[2]); } Else { returnnull; } }
Or no regular match, as in the following code:
function GetCookie (name) { var strcookie=document.cookie; var arrcookie=strcookie.split (";" ); for (var i=0;i<arrcookie.length;i++) { var arr=arrcookie[i].split ("="); if(arr[0]==name) { return arr[1]; } } return NULL ;}
Deletecookie
Delete Cookie:deletecookie (name) of the specified name
The function can delete a cookie with the specified name, which is implemented as follows:
function Deletecookie (name) { varnew Date (); -1); = name + "=v;expires=" + exp.toutcstring ();}
Description below: Here I used the toutcstring() method, looked at some of the online use of togmtstring () in the format of the time, but do not agree to use this method. Please use toutcstring () instead!! , and see it in the Chinese language.
Refer to Address: http://www.w3school.com.cn/js/js_cookies.asp
JS in Document.cookie detailed