现在的位置: 首页 > 综合 > 正文

jQuery.cookie设置说明

2014年08月19日 ⁄ 综合 ⁄ 共 2028字 ⁄ 字号 评论关闭

<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

抱歉!评论已关闭.