zhenjing

浏览器设置cookie

2015-03-11 17:43  zhenjing  阅读(588)  评论(0编辑  收藏  举报
 
// Zepto.cookie plugin
// 
// Copyright (c) 2010, 2012 
// @author Klaus Hartl (stilbuero.de)
// @author Daniel Lacy (daniellacy.com)
// 
// Dual licensed under the MIT and GPL licenses:
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html
;(function($){
    $.extend($, {
        cookie : function (key, value, options) {
            var days, time, result, decode;

            options = options || {};

            if(!options.hasOwnProperty(\'path\')){
                options.path = \'/\';
            }

            // A key and value were given. Set cookie.
            if (arguments.length > 1 && String(value) !== "[object Object]") {
                // Enforce object
                options = $.extend({}, options);

                if (value === null || value === undefined) options.expires = -1;

                if (typeof options.expires === \'number\') {
                    days = (options.expires * 1000);
                    time = options.expires = new Date();

                    time.setTime(time.getTime() + days)
                }

                value = String(value);

                return (document.cookie = [
                    encodeURIComponent(key), \'=\',
                    options.raw ? value : encodeURIComponent(value),
                    options.expires ? \'; expires=\' + options.expires.toUTCString() : \'\',
                    options.path ? \'; path=\' + options.path : \'\',
                    options.domain ? \'; domain=\' + options.domain : \'\',
                    options.secure ? \'; secure\' : \'\'
                ].join(\'\'))
            }

            // Key and possibly options given, get cookie
            options = value || {};

            decode = options.raw ? function (s) { return s } : decodeURIComponent;

            return (result = new RegExp(\'(?:^|; )\' + encodeURIComponent(key) + \'=([^;]*)\').exec(document.cookie)) ? decode(result[1]) : null
        }

    })
})(Zepto);

 

分类:

技术点:

相关文章:

  • 2021-06-06
  • 2022-12-23
  • 2021-11-19
  • 2021-12-07
  • 2021-12-07
  • 2021-11-27
  • 2021-05-30
  • 2021-11-08
猜你喜欢
  • 2021-11-27
  • 2021-12-29
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
相关资源
相似解决方案