Jquery cookie plug-in code class

Source: Internet
Author: User
Tags set cookie

Provides convenient methods to operate cookies: CopyCode The Code is as follows: $. Cookie (the _ cookie); // obtain the cookie
$. Cookie (the _ cookie, The _ value); // set the cookie
$. Cookie (the _ cookie, The _ value, {expires: 7}); // set a cookie with time
$. Cookie (the _ cookie, The _ value, {expires: 7, path: '/', domain: 'sosuo8. com ', secure: true });
$. Cookie (the _ cookie ', '', {expires:-1}); // Delete
$. Cookie (the _ cookie, null); // delete a cookie

Code:Copy codeThe Code is as follows :/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* Http://www.opensource.org/licenses/mit-license.php
* Http://www.gnu.org/licenses/gpl.html
*
*/

/**
* Create a cookie with the given name and value and other optional parameters.
*
* @ Example $. Cookie (the _ cookie, The _ value ');
* @ DESC set the value of a cookie.
* @ Example $. cookie (the _ cookie, The _ value, {expires: 7, path: '/', domain: 'jquery. com ', secure: true });
* @ DESC create a cookie with all available options.
* @ Example $. Cookie (the _ cookie, The _ value ');
* @ DESC create a session cookie.
* @ Example $. Cookie (the _ cookie, null );
* @ DESC delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* Used when the cookie was set.
*
* @ Param string name the name of the cookie.
* @ Param string value the value of the cookie.
* @ Param object options an object literal containing key/value pairs to provide optional cookie attributes.
* @ Option number | date expires either an integer specifying the expiration date from now on in days or a date object.
* If a negative value is specified (e.g. A date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* When the browser exits.
* @ Option string path the value of the path atribute of the cookie (default: path of page that created the cookie ).
* @ Option string domain the value of the domain attribute of the cookie (default: domain of page that created the cookie ).
* @ Option Boolean secure if true, the secure attribute of the cookie will be set and the cookie transmission will
* Require a secure protocol (like https ).
* @ Type undefined
*
* @ Name $. Cookie
* @ Cat plugins/cookie
* @ Author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given name.
*
* @ Example $. Cookie (the _ cookie ');
* @ DESC get the value of a cookie.
*
* @ Param string name the name of the cookie.
* @ Return the value of the cookie.
* @ Type string
*
* @ Name $. Cookie
* @ Cat plugins/cookie
* @ Author Klaus Hartl/klaus.hartl@stilbuero.de
*/
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
}
// Caution: needed to parenthesize options. Path and options. Domain
// In the following expressions, otherwise they evaluate to undefined
// In the packed version for some reason...
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;
}
};

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.