The function is assembled by analyzing the document. cookie string.
Review the operations on cookies in javascript:
You can use document. cookie = "userId = 111"; to add a cookie.
The full version can be used:Document. cookie = "userId = 111; domain = .google.com; path = \; secure = secure; expire =" + date. toGMTString ();
You can set the cookie expiration time, domain name, and path.
You only need to delete it before setting the expire time to the current time.
Now I am modifying the javascript. cookie. js class
Copy codeThe Code is as follows :/*
Cookie helper class
Easy to write, get, delete
*/
Var myCookie = {
Get: function (name ){
If (typeof name! = "Undefined ")
{
// If name given call the get value function
Return myCookie_get (name );
} Else {
// If name is not given, I want get all the cookie item
Return myCookie_getAll ();
}
},
Add: function (name, value, options ){
// Write the cookie
MyCookie_add (name, value, options );
},
Delete: function (name ){
// Delete the cookie
MyCookie_add (name, null );
}
}
String. prototype. Trim = function ()
{
Return this. replace (/^ \ s +/g, ""). replace (/\ s + $/g ,"");
}
/*
Cookie write function
@ Name: the cookie name not null
@ Value: the cookie value null = delete the cookie
@ Option: {"expires": expire time; "path":/; "domain": localhost; "secure": secure}
*/
Function myCookie_add (name, value, options)
{
If (typeof value! = 'Undefined') {// name and value given, set cookie
Options = options | {};
If (value = null ){
Value = '';
Options. expires =-1;
}
Var expires = '';
If (options. expires & (typeof options. expires = 'number' | options. expires. toUTCString )){
Var date;
If (typeof options. expires = 'number '){
Date = new Date ();
Date. setTime (date. getTime () + (options. expires * 24x60*60*1000 ));
} Else {
Date = options. expires;
}
Expires = '; expires =' + date. toUTCString (); // use expires attribute, max-age is not supported by IE
}
Var path = options. path? '; Path =' + options. path :'';
Var domain = options. domain? '; Domain =' + options. domain :'';
Var secure = options. secure? '; Secure ':'';
Document. cookie = [name, '=', encodeURIComponent (value), expires, path, domain, secure]. join ('');
}
}
/*
Get the name cookie
@ Name: the cookie's name
*/
Function myCookie_get (name)
{
Var cookieValue = null;
If (document. cookie & document. cookie! = ''){
Var cookies = document. cookie. split (';');
For (var I = 0; I <cookies. length; I ++ ){
Var cookie = cookies [I]. Trim ();
// Does this cookie string begin with the name we want?
If (cookie. substring (0, name. length + 1) = (name + '= ')){
CookieValue = decodeURIComponent (cookie. substring (name. length + 1 ));
Break;
}
}
}
Return cookieValue;
}
/*
Get all the cookie return as a json
*/
Function myCookie_getAll ()
{
Var cookieArray = new Array ();
Var str = "";
Var temp;
If (document. cookie & document. cookie! = ''){
Var cookies = document. cookie. split (';');
For (var I = 0; I <cookies. length; I ++ ){
Var cookie = cookies [I]. Trim ();
Temp = cookie. split ('= ');
// Take
CookieArray. push ("{\" name \ ": \" "+ decodeURIComponent (temp [0]) +" \ ", \" value \": \ "" + decodeURIComponent (temp [1]) + "\"}");
}
Str = cookieArray. join (",");
}
Str = "[" + str + "]";
Return eval ('+ str + ')');
}
The call is also quite simple.Copy codeThe Code is as follows: myCookie. add ("useraccount", "admin", {"expires": 5}); // add a cookie with a life cycle of 5 days
Alert (myCookie. get ("useraccount"); // retrieve the cookie
Cookies = myCookie. get (); // get all cookies
For (var I = 0; I <cookies. length; I ++)
{
Alert (cookies [I] ["name"] + ":" + cookies [I] ["value"]);
}
MyCookie. delete ("useraccount"); // delete the cookie you just added
Alert (myCookie. get ("useraccount "));