Code:
Copy Code code 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 = $.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 * 24 * 60 * 60 * 1000));
} else {
date = Options.expires;
}
expires = '; Expires= ' date.toutcstring (); Use expires attribute, Max-age isn't supported by IE
}
Note 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;
}
};
How to use
jquery Operation Cookie Plug-ins, the approximate use of the following methods
$.cookie (' The_cookie '); Read Cookie value
$.cookie (' The_cookie ', ' the_value '); Set the value of a cookie
$.cookie (' The_cookie ', ' The_value ', {expires:7, path: '/', Domain: ' jquery.com ', secure:true});/create a new cookie including the expiration path domain name, etc.
$.cookie (' The_cookie ', ' the_value '); New Cookie
$.cookie (' The_cookie ', null); Delete a cookie
Set a cookie with a name called a blog, value css9.net:
$.cookie ("blog", "Css9.net");
Set a cookie named blog, value Css9.net, and set the expiration Time (expires property) to 7 days:
$.cookie ("blog", "Css9.net", {expires:7});
Set a cookie named blog with a value of css9.net, set the expiration Time (expires property) to 7 days, and set the cookie's Path property to "/admin"
$.cookie ("blog", "Css9.net", {path: '/admin ', expires:7});
Read cookies:
To read the cookie value named blog:
Alert ($.cookie ("blog"));
To delete a cookie:
$.cookie ("example", null);