悠闲的漫步的个人主页

http://bbs.dragonsoul.top/u.php?uid=117  [收藏] [复制]

悠闲的漫步

世界是美好的,空气是清新的

  • 8

    关注

  • 16

    粉丝

  • 56

    访客

  • 等级:精灵王
  • 身份:论坛版主
  • 总积分:3906
  • 男,1980-04-26

最后登录:2017-01-02

更多资料

日志

jQuery 操作Cookie 的函数

2010-09-29 15:23
  1. 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[i]);
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    };


调用:

  1. $(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);                              
    });
    });
分类:默认分类|回复:0|浏览:543|全站可见|转载
 

Powered by phpwind v8.7.1 Certificate Copyright Time now is:05-18 18:19
©2003-2011 龍魂公会 版权所有 Gzip enabled 闽ICP备08004643号-3 Total 0.142072(s)