Jquery cookie plug-in code class

Source: Internet
Author: User
Tags set cookie

Provides convenient methods to operate cookies:
Copy codeThe 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;
}
};

Related Article

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.