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";
If you want to store more than one name/value pair at a time, you can use a semicolon plus a space (; ) separated, for example:
Document.cookie= "userid=828; Username=hulk ";
You cannot use a semicolon (;), a comma (,), an equal sign (=), and a space in the name or value of a cookie. Do it in the name of the cookie
It's easy to get to this point, but the value to save is indeterminate. How do you store these values? The method is encoded with the escape () function, which can use hexadecimal notation for some special symbols, such as spaces that will be encoded as "20%", which can be stored in
Cookie value, and the use of this scheme can also avoid the appearance of Chinese garbled. For example:
Document.cookie= "str=" +escape ("I love Ajax");
Equivalent:
Document.cookie= "Str=i%20love%20ajax";
When you use Escape () encoding, you need to use unescape () to decode the value after it is removed to get the original cookie value.
Although Document.cookie looks like a property, you can assign different values. 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";
Document.cookie= "Username=hulk";
At this point the browser will maintain two cookies, namely UserID and username, so give document.cookie a value like executing a statement like this:
Document.addcookie ("userid=828");
Document.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 Strcookie=document.cookie;
This will get a string of multiple name/value pairs separated by semicolons that includes all cookies under that domain name
<script language= "JavaScript" type= "Text/javascript" >
<!--
Document.cookie= "userid=828";
Document.cookie= "Username=hulk";
var Strcookie=document.cookie;
alert (Strcookie);
-
</script>
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:
<script language= "JavaScript" type= "Text/javascript" >
<!--
Set two cookies
Document.cookie= "userid=828";
Document.cookie= "Username=hulk";
Get cookie String
var Strcookie=document.cookie;
Cutting multi-Cookie into multiple name/value pairs
var arrcookie=strcookie.split (";");
var userId;
Iterate through the cookie array and process each cookie against
for (Var i=0;i<arrcookie.length;i++) {
var arr=arrcookie[i].split ("=");
Find the cookie with the name UserID and return its value
if ("UserId" ==arr[0]) {
USERID=ARR[1];
Break
}
}
alert (USERID);
-
</script>
In a similar way, you can get the value of one or more cookies, and the main technique is still related to string and array operations.
set end date for cookies
Until now, all cookies are single-session cookies, which will be lost when the browser is closed, in fact, these cookies are stored in memory only, and no corresponding hard disk files are created.
In real-world development, cookies often require long-term preservation, such as saving the user's login status. This can be accomplished with the following options:
Document.cookie= "userid=828; Expires=gmt_string ";
Where Gmt_string is a time string in GMT format, this statement is to set the UserID this cookie to
Gmt_string indicates that the expiration time, more than this time, the cookie will disappear and inaccessible. For example, if you want to add a cookie
Set to expire after 10 days, this can be done:
<script language= "JavaScript" type= "Text/javascript" >
<!--
Get current time
var date=new date ();
var expiredays=10;
Set date to 10 days later
Date.settime (Date.gettime () +expiredays*24*3600*1000);//or can also be used Date.setdate (date.getdate+expiredays)
Set the UserID and username two cookies to expire after 10 days
Document.cookie= "userid=828; Username=hulk; Expire= "+date.togmtstring ();
-
</script>
Delete Cookies
In order to delete a cookie, you can set its expiration time to a past time, for example:
<script language= "JavaScript" type= "Text/javascript" >
<!--
Get current time
var date=new date ();
Set date to the past time
Date.settime (Date.gettime ()-10000);
Delete the UserID cookie
Document.cookie= "userid=828; Expire= "+date.togmtstring ();
-
</script>
specify the path to an accessible cookie
By default, if a cookie is created on a page, the cookie is also accessible to other pages in the same directory as the page. If there are subdirectories under this directory, they can also be accessed in subdirectories. For example, in
Cookies created in the 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=/";
Specify the host name of the accessible cookie
Similar to paths, host names refer to different hosts under the same domain, for example: www.google.com and gmail.google.com are two different host names. By default, a cookie created in one host cannot be accessed under another host, but it can be controlled through the domain parameter in the following syntax format:
Document.cookie= "Name=value; Domain=cookiedomain ";
Take Google, for example, to achieve cross-host access, can be written as:
Document.cookie= "name=value;domain=.google.com";
This allows the cookie to be accessed by all hosts under Google.com.
Composite example: Constructing a common cookie handler function
The process of cookie processing is more complicated and has some similarity. Therefore, several functions can be defined to complete the common use of cookies.
Operation, which enables the reuse of code. The following is a list of commonly used cookie operations and their function implementations.
1. Add a Cookie:addcookie (name,value,expirehours)
The function receives 3 parameters: The cookie name, the cookie value, and the number of hours after which it expires. There is no expiration time when the expirehours is 0, i.e. the cookie disappears automatically when the browser is closed. The function is implemented as follows:
<script language= "JavaScript" type= "Text/javascript" >
<!--
function Addcookie (name,value,expirehours) {
var cookiestring=name+ "=" +escape (value);
Determine if the expiration time is set
if (expirehours>0) {
var date=new date ();
Date.settime (date.gettime+expirehours*3600*1000);
Cookiestring=cookiestring+ "; Expire= "+date.togmtstring ();
}
document.cookie=cookiestring;
}
-
</script>
2. 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:
<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 "";
}
-
function GetCookie (cookie_name) {
var results = Document.cookie.match (' (^|;)? ' + cookie_name + ' = ([^;] *) (; |$) ');
if (results)
Return (Unescape (results[2));
Else
return null;
}
</script>
3. Delete Cookie:deletecookie (name) of the specified name
The function can delete a cookie with the specified name, which is implemented as follows:
<script language= "JavaScript" type= "Text/javascript" >
<!--
function Deletecookie (name) {
var date=new date ();
Date.settime (Date.gettime ()-10000);
Document.cookie=name+ "=V; Expire= "+date.togmtstring ();
}
-
</script>
about how to use Document.cookie