热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

jquerycookie的用法总结

本篇文章主要是对jquerycookie的用法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助

jQuery COOKIE是个很好的COOKIE插件,大概的使用方法如下
example $.COOKIE('name', ‘value');
设置COOKIE的值,把name变量的值设为value
example $.COOKIE('name', ‘value', {expires: 7, path: ‘/', domain: ‘jquery.com', secure: true});
新建一个COOKIE 包括有效期 路径 域名等
example $.COOKIE('name', ‘value');
新建COOKIE
example $.COOKIE('name', null);
删除一个COOKIE

var account= $.COOKIE('name');
取一个COOKIE(name)值给myvar

代码如下

代码如下:

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(); // use expires attribute, max-age is not supported by IE
        }
        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                 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;
    }
};

然后看了下Discuz!中对COOKIE的操作方法
如下,发现少了个遍历用;分割的数组的处理
代码如下:

function getCOOKIE(name) {
var COOKIE_start = document.COOKIE.indexOf(name);
var COOKIE_end = document.COOKIE.indexOf(";", COOKIE_start);
return COOKIE_start == -1 ? '' : unescape(document.COOKIE.substring(COOKIE_start + name.length + 1, (COOKIE_end > COOKIE_start ? COOKIE_end : document.COOKIE.length)));
}
function setCOOKIE(COOKIEName, COOKIEValue, seconds, path, domain, secure) {
var expires = new Date();
expires.setTime(expires.getTime() + seconds);
document.COOKIE = escape(COOKIEName) + '=' + escape(COOKIEValue)
+ (expires ? '; expires=' + expires.toGMTString() : '')
+ (path ? '; path=' + path : '/')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
}


推荐阅读
author-avatar
萌萌蚂蚁
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有