0

我正在尝试减少我网站上的 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);

如果我只想将它用于读取 cookie,而不是用于删除、写入和读取,我可以去掉更多的死重吗?

4

2 回答 2

2

好吧,使用 jQuery 插件来读取 cookie 有点愚蠢,尽管我用同一个插件做了同样的事情!

在 JavaScript 中按名称读取 cookie 的最短函数是什么?

有一些你可以使用的更短的代码。

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

于 2012-11-12T21:01:39.257 回答
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 decoderaw只需json将其设置为默认返回您想要的格式。

于 2012-11-12T21:03:38.070 回答