【转】JQuery的cookie插件

JQuery居然没有操作cookie相关的函数,搜了下官方有个cookie的插件

 简单使用方法:

  1. <html>  
  2.    <head>  
  3.      <title>JQuery-Cookie插件</title>  
  4.      <script type="text/javascript" src="jquery-1.4.js"></script>  
  5.      <script type="text/javascript" src="jquery.cookie.js"></script>  
  6.    </head>  
  7.    <body>  
  8.      <a href="#">设置cookie1</a><br>  
  9.      <a href="#">设置cookie2</a><br>  
  10.      <a href="#">获取cookie</a><br>  
  11.      <a href="#">删除cookie</a><br>  
  12.    </body>  
  13.  </html>  
  14.  <script type="text/javascript">  
  15.  $(function(){  
  16.     var COOKIE_NAME = 'test_cookie';  
  17.     //设置cookie,通过时间间隔  
  18.     $('a').eq(0).click(function() {  
  19.         $.cookie(COOKIE_NAME, 'test', { path: '/', expires: 1 });  
  20.         return false;  
  21.     });  
  22.     // 设置cookie,到期时间  
  23.     $('a').eq(1).click(function() {  
  24.         var date = new Date();  
  25.         date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));  
  26.         $.cookie(COOKIE_NAME, 'test', { path: '/', expires: date });  
  27.         return false;  
  28.     });  
  29.     // 获取 cookie  
  30.     $('a').eq(2).click(function() {  
  31.         alert($.cookie(COOKIE_NAME));  
  32.         return false;  
  33.     });  
  34.     // 删除cookie  
  35.     $('a').eq(3).click(function() {  
  36.         $.cookie(COOKIE_NAME, null, { path: '/' });  
  37.         return false;  
  38.     });  
  39.  });  
  40.  </script> 
  41.   
  42.   

  插件的源代码也很简单:

  1. jQuery.cookie = function(name, value, options) {  
  2.     if (typeof value != 'undefined') { // name and value given, set cookie  
  3.         options = options || {};  
  4.         if (value === null) {  
  5.             value = '';  
  6.             options.expires = -1;  
  7.         }  
  8.         var expires = '';  
  9.         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();  
  18.         }  
  19.         var path = options.path ? '; path=' + (options.path) : '';  
  20.         var domain = options.domain ? '; domain=' + (options.domain) : '';  
  21.         var secure = options.secure ? '; secure' : '';  
  22.         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');  
  23.     } else {  
  24.         var cookieValue = null;  
  25.         if (document.cookie && document.cookie != '') {  
  26.             var cookies = document.cookie.split(';');  
  27.             for (var i = 0; i < cookies.length; i++) {  
  28.                 var cookie = jQuery.trim(cookies[i]);  
  29.                 if (cookie.substring(0, name.length + 1) == (name + '=')) {  
  30.                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  
  31.                     break;  
  32.                 }  
  33.             }  
  34.         }  
  35.         return cookieValue;  
  36.     }  
  37. }; 
原文地址:http://www.aspxcs.net/HTML/0114192819.html

posted on 2011-04-27 20:48  雾里寻踪  阅读(363)  评论(0编辑  收藏  举报