jQuery.cookie設定說明

來源:互聯網
上載者:User

<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的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.