JQuery cookie is a good cookie plug-in. The general usage is as follows:
Example $. cookie ('name', 'value ');
Set the cookie value and set the value of the name variable to value.
Example $. cookie ('name', 'value', {expires: 7, path: '/', domain: 'jquery. com', secure: true });
Create a cookie, including the Domain Name of the validity period.
Example $. cookie ('name', 'value ');
Create cookie
Example $. cookie ('name', null );
Delete a cookie
Var account = $. cookie ('name ');
Get a cookie (name) value to myvar
The Code is as follows:
Copy codeThe Code is as follows:
JQuery. cookie = function (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 ('');
} Else {// only name given, get cookie
Var cookieValue = null;
If (document. cookie & document. cookie! = ''){
Var cookies = document. cookie. split (';');
For (var I = 0; I <cookies. length; I ++ ){
Var cookie = jQuery. trim (cookies [I]);
// 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;
}
};
Then I read the Discuz! Cookie Operation Method
As follows, it is found that the traversal is missing; the processing of the split Array
Copy codeThe Code is as follows:
Function getcookie (name ){
Var cookie_start = document. cookie. indexOf (name );
Var cookie_end = document. cookie. indexOf (";", cookie_start );
Return cookie_start =-1? '': Unescape (document. cookie. substring (cookie_start + name. length + 1, (cookie_end> cookie_start? Cookie_end: document. cookie. length )));
}
Function setcookie (cookieName, cookieValue, seconds, path, domain, secure ){
Var expires = new Date ();
Expires. setTime (expires. getTime () + seconds );
Document. cookie = escape (cookieName) + '=' + escape (cookieValue)
+ (Expires? '; Expires =' + expires. toGMTString ():'')
+ (Path? '; Path =' + path :'/')
+ (Domain? '; Domain =' + domain :'')
+ (Secure? '; Secure ':'');
}