//使用方法如下:
//设置cookie的名值对
//$.cookie(’name’, ‘value’);
//设置cookie的名值对,有效期,路径,域,安全
//$.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});
//新建一个cookie 包括有效期 路径 域名等
//读取cookie的值
//var account= $.cookie(‘name’);
//删除一个cookie
//example $.cookie(’name’, null);
 
jQuery.cookie = function(name, value, options) {
02     if (typeof value != 'undefined') { // name and value given, set cookie
03         options = options || {};
04         if (value === null) {
05             value = '';
06             options.expires = -1;
07         }
08         var expires = '';
09         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
10             var date;
11             if (typeof options.expires == 'number') {
12                 date = new Date();
13                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
14             } else {
15                 date = options.expires;
16             }
17             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
18         }
19         // CAUTION: Needed to parenthesize options.path and options.domain
20         // in the following expressions, otherwise they evaluate to undefined
21         // in the packed version for some reason...
22         var path = options.path ? '; path=' + (options.path) : '';
23         var domain = options.domain ? '; domain=' + (options.domain) : '';
24         var secure = options.secure ? '; secure' : '';
25         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
26     } else { // only name given, get cookie
27         var cookieValue = null;
28         if (document.cookie && document.cookie != '') {
29             var cookies = document.cookie.split(';');
30             for (var i = 0; i < cookies.length; i++) {
31                 var cookie = jQuery.trim(cookies[i]);
32                 // Does this cookie string begin with the name we want?
33                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
34                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
35                     break;
36                 }
37             }
38         }
39         return cookieValue;
40     }
41 };
这是掌握cookie最后的一个障碍:缺省情况下cookie只能被在
同一个Web服务器上同一个路径下设置了该cookie的网页读取.
例如,如果在
"http://kenny.safeter.com/food/kenny/banana_kenny.htm"
有一段Javascript询问了用户的姓名,你可能需要在你的另一
个网页例如主页中访问一个给定的名字.所以你必须设定该
cookie的路径.路径"path"用于设置可以读取一个cookie的最
顶层的目录.将cookie的路径设置为你的网页最顶层的目录可
以让该该目录下的所有网页都能访问该cookie.
方法:在你的cookie中加入path=/; 如果你只想让"food" 目录
中的网页可以使用该cookie,则你加入path=/food;.还有一点:
有些网站有许多小的域名,例如网猴可能还在
"chimp.webmonkey.com," "gorilla.webmonkey.com," 和
"ape.webmonkey.com." 域名下有网页.缺省情况下只有
"chimp.webmonkey.com" 域下的网页可以读取该cookie.如果
你向让"webmonkey.com"下的所有机器都可以读取该cookie,我
们必须在cookie中加入 "domain=webmonkey.com" .

要将一个cookie设置在
"http://chimp.webmonkey.com/food/bananas/banana_puree.htm" 
并且让所有网猴的网页都可以利用它,我们可以这样:
jquery.cookie使用方法_title代码
function setCookie()

{

    
var the_name = prompt("What's your name?",""
);

    
var the_cookie ="cookie_puss=" + escape(the_name) + ";"
 ;

    
var the_cookie = the_cookie+ "path=/;"
;

    
var the_cookie = the_cookie + "domain=webmonkey.com;"
;


    document.cookie 
=
the_cookie;

}