返回顶部

收藏

jQuery cookie插件读写cookie

更多
$.cookie('the_cookie'); // get cookie
$.cookie('the_cookie', 'the_value'); // set cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); // set cookie with an expiration date seven days in the future
$.cookie('the_cookie', '', { expires: -1 }); // delete cookie
$.cookie('the_cookie', null); // delete cookie

jquery.cookie.js 代码:

 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
        }
        // CAUTION: 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;
    }
};

标签:javascript,jquery,cookie

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Leon 发表 2018-03-28 14:10:00 本地使用js或jquery操作cookie在谷歌浏览器chrome中不生效
  2. phpvar 发表 2014-11-20 10:16:54 js 与或运算符 || && 应用技巧
  3. w3cui 发表 2014-08-20 02:48:23 JavaScript异步编程的模式
  4. 张映 发表 2015-02-11 09:11:36 jquery js 动态加载 js文件
  5. kn007 发表 2015-02-11 05:32:35 新浪微博关注按钮遇到坑的解决法子
  6. 博主 发表 2014-11-20 04:06:18 再谈弹窗那些事
  7. chirs <> 发表 2009-01-05 13:18:12 基于jQuery的表单验证插件
  8. terry 发表 2013-03-01 04:57:01 超酷的响应式dribbble设计作品瀑布流布局效果
  9. Hiro 发表 2010-02-26 07:17:13 jQuery:丰富效果的Flash图片播放(JS重写版)
  10. maisui99 发表 2012-10-18 02:54:25 为什么我要重拾K2
  11. 刘晓帆 发表 2013-08-09 07:45:15 jQuery的on()方法支持的所有事件
  12. Jessica 发表 2011-10-09 10:54:34 关于JS预解析一道题

发表评论