设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 363|回复: 0
打印 上一主题 下一主题

jQuery 操作Cookie 的函数 [复制链接]

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    我玩的应用:

    跳转到指定楼层
    楼主
    发表于 2012-4-27 16:53:59 |只看该作者 |倒序浏览

    jQuery.cookie = function(name, value, options) {
              if (typeof value != 'undefined') {
                        options = options || {};
                        if (value === null) {
                                  value = '';
                                  options = $.extend({}, options);
                                  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);
                                            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                                                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                                                      break;
                                            }
                                  }
                        }
                        return cookieValue;
              }
    };




    调用方法!

    $(document).ready(function() {
    $('#wCookies').click(function(){
         $.cookie('name', 'test',{expires: 7});                        
    });
    $('#rCookies').click(function(){
         var test = $.cookie('name');
         alert (test);
    });
    $('#dCookies').click(function(){
         $.cookie('name', null);                                       
    });
    });



    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-3 20:47 , Processed in 0.097443 second(s), 29 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部