I checked some information on the Internet, wrote the following example, and shared it together. But there is another problem to solve:
In the following code, you can add, delete, modify, and query IE6 and Firefox browsers. However, if document. Cookie =
Cookiename + '=' + getcookies (cookiename) + "; expires =" + (New
Date (0). toutcstring () + '; Path =/'; add
Domain, secure
The deletion may fail. (For example
Cookiename + '=' + getcookies (cookiename) + "; expires =" + (New
Date (0 )). toutcstring () + '; Path =/' + '; domain =' + '; secure'; please be grateful to anyone who knows the mysteries of the solution and can follow it directly.
.
Expires = '+ date. toutcstring () +'; Path = /';
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page
Import = "Java. Io. *" %>
<% @ Page import = "javax. servlet. *" %>
<% @
Page import = "javax. servlet. http. *" %>
<% @ Page
Import = "Java. util. *" %> <HTML>
<Head>
<Title> test page </title>
<Script
Type = "text/JavaScript">
//
Add Cookie Information
Function savecookies (){
VaR cookiekey =
Document. getelementbyid ('cookiekey ');
VaR cookievalue =
Document. getelementbyid ('cookievalue '); If (! Navigator. cookieenabled) {// does the browser support cookies?
Alert ('no cookie! ');
} Else
If (isblankcheck (cookiekey. Value) {// The cookie key is empty.
Alert ('enter key ');
Cookiekey. Focus ();
} Else
If (isblankcheck (cookievalue. Value) {// the cookie value is null.
Alert ('Enter the value ');
Cookievalue. Focus ();
} Else {// set cookie
VaR
Date = new date ();
// Set the expiration time
(3 minutes)
Date. settime (date. gettime () + 3*60000 );
Document. cookie = escape (cookiekey. value) + '=' + escape (cookievalue. value) + '; expires =' + date. toutcstring () + '; Path = /';
}
} // Obtain the specified Cookie Information
Function
Getcookies (cookie_name ){
VaR retvalue = '';
VaR allcookies =
Document. Cookie;
VaR cookie_pos = allcookies. indexof (cookie_name); // If an index is found, the cookie exists,
// Otherwise, it indicates that it does not exist.
If (cookie_pos! =
-1)
{
// Put cookie_pos at the beginning of the value. You only need to add 1 to the value.
Cookie_pos ++ =
Cookie_name.length + 1; var cookie_end = allcookies. indexof (";", cookie_pos); If (cookie_end =-1 ){
Cookie_end = allcookies. length;
} Retvalue = Unescape (allcookies. substring (cookie_pos,
Cookie_end ));
} Return retvalue;
}
// Obtain all cookies
Function getallcookies (){
Return document. Cookie;
}
// Delete Cookie Information
Function deletecookies ()
{Var delcookienameobj = Document. getelementbyid ('delcookiename ');
VaR
Delcookiename = delcookienameobj. value; If (! Navigator. cookieenabled) {// does the browser support cookies?
Alert ('no cookie! ');
} Else
If (isblankcheck (delcookiename) {// The cookie key is empty
Alert ('enter key ');
Delcookienameobj. Focus ();
} Else
If (isiankcheck (getcookies (delcookiename ))){
Alert ('this cookie does not exist or has been deleted. Please check and confirm ');
} Else {// Delete cookie
VaR
Date = new
Date ();
// Delete a cookie, that is, set its expiration time to a previous time.
Date. settime (date. gettime ()
-4*60000 );
// Document. cookie = 'username = '+'; expires = '+ date. togmtstring () + '; Path =/' + '; domain =' + '; secure ';
// Document. Cookie = "username" + "=; expires = Thu, 01-Jan-1970 00:00:01
GMT ";
Document. Cookie =
Delcookiename + '=' + getcookies (delcookiename) + '; expires =' + (New
Date (0). toutcstring () + '; Path = /';
Showallcookies ();
}
} // Set cookie expiration
Function makecookiesexpired ()
{
Alert ('3 minutes later, the cookie expires, and then click show all cookies to view ');
} // Display the specified Cookie Information
Function
Showonecookie (cookiename ){
VaR displaycookienameobj =
Document. getelementbyid (cookiename );
VaR displaycookiename =
Displaycookienameobj. value;
If (! Navigator. cookieenabled) {// does the browser support cookies?
Alert ('no cookie! ');
} Else
If (isblankcheck (displaycookiename) {// The cookie key is empty
Alert ('enter key ');
Displaycookienameobj. Focus ();
} Else
If (isblankcheck (getcookies (displaycookiename ))){
Alert ('this cookie does not exist or has been deleted. Please check and confirm ');
} Else {
Document. getelementbyid ("cookieresult"). innerhtml = "<span> <font
Color = Red> "+ getcookies (displaycookiename) +" </font> </span>"
}
} // Display all Cookie Information
Function
Showallcookies (){
Document. getelementbyid ("cookieresult"). innerhtml = "<span> <font
Color = Red> "+ getallcookies () +" </font> </span>"
}
// Null String judgment (null: True/not blank: false)
Function
Isblankcheck (value ){
VaR ret =
True;
If (TRIM (value )! = ''){
Ret = false;
} Return ret;
} // Delete spaces between the left and right sides
Function trim (STR ){
Return
Str. Replace (/(^/S *) | (/S * $)/g ,"");
} // Delete the space on the left
Function ltrim (STR ){
Return
Str. Replace (/(^/S *)/g ,"");
} // Delete the right space
Function rtrim (STR ){
Return
Str. Replace (/S * $)/g ,"");
}
</SCRIPT>
</Head>
<Body>
<
Href = "index.html"> return to the home page </a> <br/>
Note:
<Ol>
<Li>
Enter the key and value of the cookie to save the cookie.
</LI>
<Li>
Show all cookies and view all cookies
</LI>
<Li>
Enter the cookie key to view the current cookie value.
</LI>
<Li>
Enter the cookie key to delete the cookie.
</LI>
</OL> <Form ID = "form1" name = "form1" method = "Post"
Action = "#">
<Input type = "button" name = "Submit"
Value = "show all cookies"
Onclick = "showallcookies ()"/>
<Input
Type = "button" name = "submit2"
Value = "Cookie expired"
Onclick = "makecookiesexpired ()"
/> <Br/>
Input key:
<Input type = "text" name = "cookiekey"
/> <Br/>
Input value:
<Input type = "text" name = "cookievalue"
/>
<Input type = "button" name = "cookieenabled"
Value = "Save cookie"
Onclick = "savecookies ()"
/> <Br/>
Enter the viewing key:
<Input type = "text"
Name = "displaycookiename"/>
<Input type = "button" name = "submit1"
Value = "show specified cookie"
Onclick = "showonecookie ('displaycookiename ');"
/>
<Br/>
Enter the deletion key:
<Input type = "text"
Name = "delcookiename"/>
<Label>
<Input type = "button"
Name = "submit3" value = "delete cookie"
Onclick = "deletecookies ()"
/>
</Label>
<Br/>
</Form>
<HR
/>
<Div id = "cookieresult">
<Font
Color = "green"> display cookie information area </font>
</Div>
</Body>
</Html> description:
In a Javascript script, a cookie
It is actually a string attribute. When you read the cookie value, you get a string containing the names and values of all cookies used on the current web page. Each cookie
Besides the name and value attributes, there are four attributes. These attributes are: expires expiration time, Path,
Domain domain and secure security
.
Expires
-
Expiration time. Specifies the life cycle of the cookie. Specifically, the value is the expiration date. This attribute is required if you want to allow the cookie to exist for more than the current browser session time. When the expiration date expires, the browser can delete the cookie file without any impact.
Path
-Path. Specifies the web page associated with the cookie. The value can be a directory or a path. If
Http://www.jzxue.com/html/java/
Create a cookie
Http://www.jzxue.com/html/java/
All pages in the directory and pages in any sub-directories under the directory can access this cookie. That is to say, in
Http://www.jzxue.com/html/java/stories/articles
Any page in
Http://www.jzxue.com/html/java/index.html
Cookie. However, if
Http://www.jzxue.com/html/asp/
Access required
Http://www.jzxue.com/html/java/index.html
How to Set cookes? In this case, we need
Set the path attribute to "/". When specifying a path, all web pages with the same path in the URL from the same server can share cookies. Now let's look at another example: if you want
Http://www.jzxue.com/devhead/filters/
And
Http://www.jzxue.com/devhead/stories/
To share cookies, you must set path to "/devhead ".
Domain
-
Domain. Specify the associated web server or domain. The value is a domain name, such as jzxue.com. This is an extension of the path attribute. If we want catalog.mycompany.com
What should I do if I can access the cookies set by shoppingcart.mycompany.com?
We can set the domain attribute to "mycompany.com" and the path attribute to "/". FYI: you cannot set the cookie domain attribute to a value different from the domain of the server on which it is set.
. (The reason for the above failure
)
Secure
-
Security. Specify how the cookie value is transmitted between the user and the Web server over the network. The value of this attribute is either "secure" or empty. By default, this attribute is null, that is, data is transmitted using an insecure HTTP connection. If
Cookie
When it is marked as secure, it transmits data with the Web server over https or other security protocols. However, setting the secure attribute does not mean that others cannot see the cookies saved locally on your machine. In other words, setting the cookie as secure only ensures that the data transmission process between the cookie and the Web server is encrypted and stored locally
Cookie files are not encrypted. If you want to encrypt the local cookie, you must encrypt the data yourself.