Set cookie
Each cookie is a name/value pair. You can assign a string below to the Document.cookie:
document.cookie = "userid = 828";
If you want to store multiple name/value pairs at a time, you can use the septum plus the space (;) to separate, for example:
document.cookie = "userid = 828; username = hulk";
You cannot use a segment number (;), comma (,), equivalent (=) and space in the name or value of Cookie. It is easy to do this in the name of cookies, but the value to be preserved is uncertain. How to store these values? The method is to encode it with the Escape () function. It can use some special symbols to indicate by hexadecimal. For example, spaces will be encoded as "20%", which can be stored in the cookie value, and it can be stored in the Cookie value, and it can also be stored in the Cookie value, and it can also be stored in the Cookie value, and it can also be stored in the cookie value, and it can be stored in the cookie value, and it can also be stored in the cookie value, and it can also be stored in the cookie value, and it can be stored in the cookie value, and it can be stored in the cookie value, and it can be stored in the cookie value, and it can be stored in the cookie value, and it can be stored. Using this scheme can also avoid the emergence of Chinese garbled. For example:
document.cookie = "Str ="+Escape ("I Love Ajax");
Equivalent to:
document.cookie = "Str = I%20love%20AJAX";
After using Escape () coding, you need to use UNESCAPE () to decoding after taking the value to obtain the original cookie value, which has been introduced earlier.
Although Document.cookie looks like a attribute, it can give different values. But it is not the same as the general attribute. Change its assignment does not mean losing the original value. For example, the following two statements are continuously executed:
document.cookie = "userid = 828";
document.cookie = "username = hulk";
At this time, the browser will maintain two cookies, which are Userid and Username, so they assign a value to the document.cookie more like such statements like this:
document.addcookie ("userid = 828");
document.addcookie ("username = hulk");
In fact, the browser sets cookies in this way. If you want to change the value of a cookie, you only need to restart the value, such as:
document.cookie = "userid = 929";
In this way, set the cookies named Userid to 929.
Get the value of cookies
Here are how to get the value of cookies. The value of the cookie can be obtained directly by the Document.cookie:
Var StrCookie = Document.cookie;
This will obtain multiple name/values that are separated by a segment number. The name/value is to all Cookie in the domain name. For example:
<script language = "javascript" type = "text/javascript">
<!--
document.cookie = "userid = 828";
document.cookie = "username = hulk";
Var StrCookie = Document.cookie;
alert (strCookie);
//->
</script>
Figure 7.1 Display the output cookie value. It can be seen that only one cookie value can be obtained at one time, and the cookie name cannot be specified to obtain the specified value. This is the most troublesome part of the cookie value. Users must analyze this string by themselves to obtain the specified cookie value. For example, if you want to obtain the value of userid, you can implement it like this:
<script language = "javascript" type = "text/javascript">
<!--
// Set two cookies
document.cookie = "userid = 828";
document.cookie = "username = hulk";
// Get the cookie string
Var StrCookie = Document.cookie;
// Multiple cookies are cut into multiple names/values
var arrCookie = Strcookie.split (";");
var userid;
// Traversing the cookie array, processing each cookie pair
for (var I = 0; I <arrCookie.Length; i ++) {
var aRR = ArrCookie [i] .split ("=");
// Find the cookie named Userid and return its value
if ("userid" == arr [0]) {
userid = arr [1];
Break;
}
}
alert (userid);
//->
</script>
This gets the value of a single cookie
In a similar method, you can get the value of one or more cookies, and its main techniques are still related operations of string and array.
Set the termination date for cookies
Until now, all Cookie is a single -session cookie, that is, these cookies will be lost after the browser is closed. In fact, these cookies are only stored in memory without building corresponding hard disk files.
In actual development, Cookie often needs to be stored for a long time, such as preserving user login status. This can be implemented with the following options:
document.cookie = "userid = 828; expires = gmt_string";
Among them, GMT_STRING is a time string represented by GMT format. This statement is to set the Userid this cookie to GMT_STRING's expiration time. In this time, cookies will disappear and cannot be accessed. For example: If you want to set the cookies to expire after 10 days, you can implement it like this:
Copy code code as follows:
<script language = "javascript" type = "text/javascript">
<!--
// Get the current time
var date = new date ();
var expireDays = 10;
// Set the date to 10 days after 10 days
Date.settime (date.gettime ()+ExpireDays*24*3600*1000);
// Set the two cookies of Userid and Username to expire after 10 days
document.cookie = "userid = 828; username = hulk; expire ="+date.togmtring ();
//->
</script>
Delete Cookie
In order to delete a cookie, it can set its expiration time to a past time, such as::
Copy code code as follows:
<script language = "javascript" type = "text/javascript">
<!--
// Get the current time
var date = new date ();
// Set the date as the past time
date.settime (date.gettime ()-10000);
// Delete the Userid this cookie
document.cookie = "userid = 828; expire ="+date.togmtring ();
//->
</script>
Specify the path that can access cookies
By default, if a cookie is created on a page, other pages in the directory where the page can also access the cookie. If there are sub -directory in this directory, you can also be accessed in the sub -directory. For example, the cookies created in www.xxxx.com/html/b.html or www.xxx.com/ html/some/c.html can Can't be accessed by www.xxxx.com/d.html.
In order to control the catalog that can be accessed by cookies, you need to use the PATH parameter to set cookies. The grammar is as follows:
document.cookie = "name = value; path = cookiedir";
The cookiedir represents the catalog of access to Cookie. For example:
document.cookie = "userid = 320; path =/shop";
It means that the current cookies can only be used in the short directory.
If you want to make Cookie use it under the entire website, you can specify the cookie_dir as the root directory, such as:
document.cookie = "userid = 320; path =/";
Specify the host name that can access Cookie
Similar to the path, the host name refers to different hosts in the same domain, such as: www.google.com and gmail.google.com are two different host names. By default, the cookies created in a host cannot be accessed under another host, but it can be controlled by the domain parameter to control it. The syntax format is:
document.cookie = "name = value; domain = cookomain";
Take Google as an example. To achieve cross -host access, you can write:
document.cookie = "name = value; domain = .google.com";
In this way, the host under all Google.com can access the cookie.
Comprehensive example: Constructing a common cookie processing function
Cookie's processing process is more complicated and has certain similarities. Therefore, several functions can be defined to complete the general operation of Cookie, so as to achieve code reuse. The commonly used cookie operations and function implementation are listed below.
1. Add a cookie: addcookie (name, value, expirehouse)
This function receives 3 parameters: cookie name, cookie value, and how hours it expires later. The expiration time is not set when the Expirehouse is 0, that is, the cookies automatically disappear when the browser is closed. This function is implemented as follows:
Copy code code as follows:
<script language = "javascript" type = "text/javascript">
<!--
Function AddCookie (name, value, expirehouse) {
var cookring = name+"="+escape (value);
// Determine whether the expiration time is set
if (expirehouse> 0) {{
var date = new date ();
date.settime (date.gettime+expirehouse*3600*1000);
cookring = cookieString+"; expire ="+date.togmtring ();
}
document.cookie = cookieString;
}
//->
</script>
2. Get the cookie value of the specified name: getCookie (name)
This function returns a cookie value named name name. If there is no existence, it will return to the air, and its implementation is as follows:
Copy code code as follows:
<script language = "javascript" type = "text/javascript">
<!--
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 "" ";
}
//->
</script>
3. Delete Cookie with specified name: deletecookie (name)
This function can delete the cookie of the specified name, and its implementation is as follows:
Copy code code as follows:
<script language = "javascript" type = "text/javascript">
<!--
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 "" ";
}
//->
</script>
You can also use another online:
Copy code code as follows:
<script language = "javascript" type = "text/javascript">
Function setCookie (name, value) // two parameters, one is the name of cookie, the other is the value
{{
var days = 30; // This cookie will be saved for 30 days
var exp = new date (); // new date ("December 31, 9998");
exp.settime (exp.gettime () + Days*24*60*60*1000);
document.cookie = name + "=" + escape (value) + "; expires =" + exp.togmtring ();
}
Function GetCookie (name) // Take the cookies function
{{
var anrr = document.cookie.match (new regexp ("(" (^|) "+name+" = ([^;]*) (; | $) "))));
if (Arr! = NULL) Return Unescape (ARR [2]); Return Null;
}
function delCookie (name) // Delete cookies
{{
var exp = new date ();
exp.settime (exp.gettime () - 1);
var cval = getCookie (name);
if (cval! = null) document.cookie = name+"="+cval+"; expires ="+exp.togmtstring ();
}
Setcookie ("xiaoqi", "3")
Alert (getCookie ('xiaoqi');
</script>