Jquery obtains cookie values. The cookie code is frequently used in WEB development. Next we will talk about how to use popular jquery to set cookie values, function used to obtain the cookie value.
Jquery obtains the cookie value code
Cookie is frequently encountered in web development. Next we will talk about how to use jquery, a popular one, to set the cookie value and obtain the cookie value.
Jquery. cookie = function (name, value, options ){
If (typeof value! = 'Undefined') {// name and value given, set cookie
Options = options | {};
If (value = null ){
Value = '';
Options = $. extend ({}, options); // clone object since it's unexpected behavior if the expired property were changed
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;
}
};
// Usage
$. Cookie (the _ cookie, the _ value, {expires: 7, path: '/', domain: 'bkjia. c0m', secure: true });