Jquery cookie usage

Source: Internet
Author: User
Tags set cookie

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

CodeAs 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

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 ':'');
}

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:

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

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': '');
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.