剥离Jquery Cookie脚本

时间:2012-11-12 20:56:27

标签: javascript jquery cookies

我正在尝试减少我网站上的javascript开销,并且我正在使用此插件: https://github.com/carhartl/jquery-cookie

这是代码:

/*!
 * jQuery Cookie Plugin v1.3
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2011, Klaus Hartl
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/GPL-2.0
 */
(function ($, document, undefined) {

  var pluses = /\+/g;

    function raw(s) {
        return s;
    }

    function decoded(s) {
        return decodeURIComponent(s.replace(pluses, ' '));
    }

    var config = $.cookie = function (key, value, options) {

        // write
        if (value !== undefined) {
            options = $.extend({}, config.defaults, options);

            if (value === null) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            value = config.json ? JSON.stringify(value) : String(value);

            return (document.cookie = [
                encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        // read
        var decode = config.raw ? raw : decoded;
        var cookies = document.cookie.split('; ');
        for (var i = 0, l = cookies.length; i < l; i++) {
            var parts = cookies[i].split('=');
            if (decode(parts.shift()) === key) {
                var cookie = decode(parts.join('='));
                return config.json ? JSON.parse(cookie) : cookie;
            }
        }

        return null;
    };

    config.defaults = {};

    $.removeCookie = function (key, options) {
        if ($.cookie(key) !== null) {
            $.cookie(key, null, options);
            return true;
        }
        return false;
    };

})(jQuery, document);

这是我试图删除“写”代码

来剥离它
(function ($, document, undefined) {

  var pluses = /\+/g;

function raw(s) {
    return s;
}

function decoded(s) {
    return decodeURIComponent(s.replace(pluses, ' '));
}

var config = $.cookie = function (key, value, options) {

    // read
    var decode = config.raw ? raw : decoded;
    var cookies = document.cookie.split('; ');
    for (var i = 0, l = cookies.length; i < l; i++) {
        var parts = cookies[i].split('=');
        if (decode(parts.shift()) === key) {
            var cookie = decode(parts.join('='));
            return config.json ? JSON.parse(cookie) : cookie;
        }
    }

    return null;
};

config.defaults = {};

$.removeCookie = function (key, options) {
    if ($.cookie(key) !== null) {
        $.cookie(key, null, options);
        return true;
    }
    return false;
};

})(jQuery, document);

如果我只是想用它来阅读cookies,而不是删除,写作和阅读,那么我是否可以去除它的重量。

2 个答案:

答案 0 :(得分:2)

好吧,使用jQuery插件只是为了读取一个cookie有点傻,虽然我用相同的插件做了同样的事情!

What is the shortest function for reading a cookie by name in JavaScript?

可以使用一些较短的代码。

所有这些的核心都是document.cookie - 它有点糟糕,它们只是一个字符串,而不是一个很好的数组 - 但是你去了。

答案 1 :(得分:1)

有很多。您可以为初学者删除removeCookie()。此外,您会看到$.cookie()方法接收valueoptions。如果您只是阅读cookie,那么这些是不必要的。您还将删除与它们相关的代码。我猜你可以把它归结为这样的东西:

/*!
 * jQuery Cookie Plugin v1.3
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2011, Klaus Hartl
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/GPL-2.0
 */
(function ($, document, undefined) {

    var pluses = /\+/g;

    function raw(s) {
        return s;
    }

    function decoded(s) {
        return decodeURIComponent(s.replace(pluses, ' '));
    }

    var config = $.cookie = function (key, value, options) {
        // read
        var decode = config.raw ? raw : decoded;
        var cookies = document.cookie.split('; ');
        for (var i = 0, l = cookies.length; i < l; i++) {
            var parts = cookies[i].split('=');
            if (decode(parts.shift()) === key) {
                var cookie = decode(parts.join('='));
                return config.json ? JSON.parse(cookie) : cookie;
            }
        }

        return null;
    };

    config.defaults = {};

})(jQuery, document);

根据您的使用情况,您可以删除与config decoderawjson相关的所有代码,只需将其设置为返回默认情况下您想要的格式。

相关问题