在JavaScript中按名称读取cookie的最短函数是什么? [英] What is the shortest function for reading a cookie by name in JavaScript?

查看:102
本文介绍了在JavaScript中按名称读取cookie的最短函数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中阅读Cookie的最短,准确和跨浏览器兼容的方法是什么?

独立的脚本(其中我不能有任何外部依赖),我发现我添加了一个函数读取cookie,通常落在 QuirksMode.org readCookie() 方法(280个字节,216个缩写)

Very often, while building stand-alone scripts (where I can't have any outside dependencies), I find myself adding a function for reading cookies, and usually fall-back on the QuirksMode.org readCookie() method (280 bytes, 216 minified.)

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

它的工作,但它的丑陋,并增加了一点膨胀每一次。

It does the job, but its ugly, and adds quite a bit of bloat each time.

jQuery的方法。 cookie 使用类似这样的东西(修改,165字节,125缩小):

The method that jQuery.cookie uses something like this (modified, 165 bytes, 125 minified):

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

注意竞争:我有理由减少我的readCookie函数的大小,并确保我的解决方案是有效的。

Note this is not a 'Code Golf' competition: I'm legitimately interested in reducing the size of my readCookie function, and in ensuring the solution I have is valid.

推荐答案

这只会遇到document.cookie一次。每个后续请求都将是即时的。

This will only ever hit document.cookie ONE time. Every subsequent request will be instant.

(function(){
    var cookies;

    function readCookie(name,c,C,i){
        if(cookies){ return cookies[name]; }

        c = document.cookie.split('; ');
        cookies = {};

        for(i=c.length-1; i>=0; i--){
           C = c[i].split('=');
           cookies[C[0]] = C[1];
        }

        return cookies[name];
    }

    window.readCookie = readCookie; // or expose it however you want
})();

恐怕真的没有比这个一般逻辑更快的方法,除非你自由使用 .forEach 这是与浏览器有关的(即使那样你不会节省太多)

I'm afraid there really isn't a faster way than this general logic unless you're free to use .forEach which is browser dependent (even then you're not saving that much)

稍微压缩到 120字节

function read_cookie(k,r){return(r=RegExp('(^|; )'+encodeURIComponent(k)+'=([^;]*)').exec(document.cookie))?r[2]:null;}

如果你做的话,你可以得到 110 bytes 如果你删除 encodeURIComponent ,它是一个1个字母的函数名, 90个字节

You can get it to 110 bytes if you make it a 1-letter function name, 90 bytes if you drop the encodeURIComponent.

我已经得到 73字节,但公平的是 82字节当添加 encodeURIComponent 时命名为 readCookie 102个字节

I've gotten it down to 73 bytes, but to be fair it's 82 bytes when named readCookie and 102 bytes when then adding encodeURIComponent:

function C(k){return(document.cookie.match('(^|; )'+k+'=([^;]*)')||0)[2]}

这篇关于在JavaScript中按名称读取cookie的最短函数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆