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 の削除、書き込み、読み取りではなく、Cookie の読み取りのみに使用したい場合、取り除くことができる自重は他にありますか。

4

2 に答える 2

2

ええと、Cookieを読み取るためだけにjQueryプラグインを使用するのはちょっとばかげていますが、同じプラグインで同じことをしました!

JavaScriptで名前でCookieを読み取るための最短の関数は何ですか?

使用できる短いコードがいくつかあります。

これらすべての中核となるのはdocument.cookieです。これは、すでに優れた配列ではなく、単なる文字列であるというのはちょっと残念ですが、そこに行きます。

于 2012-11-12T21:01:39.257 に答える
1

たくさんあります。removeCookie()初心者のために削除することができます。$.cookie()また、メソッドがvalueとを取り込んでいることがわかりますoptions。クッキーを読んでいるだけなら、これらは不要です。また、それらに関連するコードを削除します。私はあなたがそれをこのようなものに下げることができると推測しています:

/*!
 * 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 decodeユースケースによっては、、、、rawおよびに関連するすべてのコードを削除できる場合がありjsonます。デフォルトで必要な形式を返すように設定するだけです。

于 2012-11-12T21:03:38.070 に答える