<html>
<head>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>jQuery Cookie Plugin</title>
<script src="http://www.codefans.net/ajaxjs/jquery1.3.2.js" type="text/javascript"></script>
<script src="http://www.codefans.net/ajaxjs/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
var COOKIE_NAME = 'test_cookie';
var ADDITIONAL_COOKIE_NAME = 'additional';
$('a').eq(0).click(function() { // 用天數設定 cookie
$.cookie(COOKIE_NAME, 'test', { path: '/', expires: 10 });
return false;
});
$('a').eq(1).click(function() { // 用日期設定 cookie
var date = new Date();
date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
$.cookie(COOKIE_NAME, 'test', { path: '/', expires: date });
return false;
});
$('a').eq(2).click(function() { // 擷取 cookie
alert($.cookie(COOKIE_NAME));
return false;
});
$('a').eq(3).click(function() { // 刪除 cookie
$.cookie(COOKIE_NAME, null, { path: '/' });
return false;
});
$('a').eq(4).click(function() { // 設定第二個 cookie
$.cookie(ADDITIONAL_COOKIE_NAME, 'foo', { expires: 10 });
return false;
});
$('a').eq(5).click(function() { // 擷取第二個 cookie
alert($.cookie(ADDITIONAL_COOKIE_NAME));
return false;
});
$('a').eq(6).click(function() { // 刪除第二個 cookie
$.cookie(ADDITIONAL_COOKIE_NAME, null);
return false;
});
});
</script>
</head>
<body>
<p>
<a href="#">設定 cookie (設定有效期間天數為 10 天)</a><br>
<a href="#">設定 cookie (通過 date 對象設定到期日期為 3 天后的那天)</a><br>
<a href="#">擷取 cookie</a><br>
<a href="#">刪除 cookie</a><br>
<a href="#">設定另一個 cookie</a><br>
<a href="#">擷取另一個 cookie</a><br>
<a href="#">刪除另一個 cookie</a>
</p>
</body>
</html>
<script type="text/javascript" src="/js/jquery.cookie.js"></script>
$.cookie('the_cookie'); // get cookie
$.cookie('the_cookie', 'the_value'); // set cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); // set cookie with an expiration date seven days in the future
$.cookie('the_cookie', '', { expires: -1 }); // delete cookie
$.cookie('the_cookie'); //讀取Cookie值
$.cookie(’the_cookie’, ‘the_value’); //設定cookie的值
$.cookie(’the_cookie’, ‘the_value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});//建立一個cookie 包括有效期間 路徑 網域名稱等
$.cookie(’the_cookie’, ‘the_value’); //建立cookie
$.cookie(’the_cookie’, null); //刪除一個cookie
摘自 Jack的專欄