Provides a convenient way to manipulate cookies:
Copy Code code as follows:
$.cookie (' The_cookie '); Get cookies
$.cookie (' The_cookie ', ' the_value '); Set cookies
$.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 Cookies
Code:
Copy Code code 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 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, have to use the same path and domain
* Used when the cookie is 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/A.
* If A negative value is specified (e.g. a date in the past), the cookie would be deleted.
* If set to NULL or omitted, the cookie is a session cookie and won't 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 of the "cookie" is set and the cookie transmission would
* 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 cookies 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 * 24 * 60 * 60 * 1000));
} else {
date = Options.expires;
}
expires = '; Expires= ' + date.toutcstring (); Use expires attribute, Max-age isn't 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;
}
};