登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

pcwanli的博客

nx

 
 
 

日志

 
 

JQuery设置cookie|JQuery删除cookie|JQuery获取cookie 过期  

2010-11-24 16:47:03|  分类: javascript |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

简单使用方法:


<html>  
   <head>  
     <title>JQuery-Cookie插件</title>  
     <script type="text/javascript" src="jquery-1.4.js"></script>  
     <script type="text/javascript" src="jquery.cookie.js"></script>  
   </head>  
   <body>  
     <a href="#">设置cookie1</a><br>  
     <a href="#">设置cookie2</a><br>  
     <a href="#">获取cookie</a><br>  
     <a href="#">删除cookie</a><br>  
   </body>  
</html>  
<script type="text/javascript">  
$(function(){  
     var COOKIE_NAME = 'test_cookie';  
     //设置cookie,通过时间间隔  
     $('a').eq(0).click(function() {  
         $.cookie(COOKIE_NAME, 'test', { path: '/', expires: 1 });  
         return false;  
     });  
     // 设置cookie,到期时间  
     $('a').eq(1).click(function() {  
         var date = new Date();  
         date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));  
         $.cookie(COOKIE_NAME, 'test', { path: '/', expires: date });  
         return false;  
     });  
     // 获取 cookie  
     $('a').eq(2).click(function() {  
         alert($.cookie(COOKIE_NAME));  
         return false;  
     });  
     // 删除cookie  
     $('a').eq(3).click(function() {  
         $.cookie(COOKIE_NAME, null, { path: '/' });  
         return false;  
     });  
});  
</script>

插件的源代码也很简单:


jQuery.cookie = function(name, value, options) {  
     if (typeof value != 'undefined') { // name and value given, set cookie
          options = options || {};  
         if (value === null) {  
              value = '';  
              options.expires = -1;  
          }  
         var expires = '';  
         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {  
             var date;  
             if (typeof options.expires == 'number') {  
                  date = new Date();  
                  date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));  
              } else {  
                  date = options.expires;  
              }  
              expires = '; expires=' + date.toUTCString();  
          }  
         var path = options.path ? '; path=' + (options.path) : '';  
         var domain = options.domain ? '; domain=' + (options.domain) : '';  
         var secure = options.secure ? '; secure' : '';  
          document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');  
      } else {  
         var cookieValue = null;  
         if (document.cookie && document.cookie != '') {  
             var cookies = document.cookie.split(';');  
             for (var i = 0; i < cookies.length; i++) {  
                 var cookie = jQuery.trim(cookies[i]);  
                 if (cookie.substring(0, name.length + 1) == (name + '=')) {  
                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  
                     break;  
                  }  
              }  
          }  
         return cookieValue;  
      }  
};

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <title>jQuery Cookie Plugin</title>
        <script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
        <script src="../../scripts/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>

  评论这张
 
阅读(3158)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018