Javascript getCookie函数

Javascript getCookie函数,javascript,cookies,Javascript,Cookies,我发现有两个函数可以使用Javascript获取cookie数据,一个在上,另一个在 我想知道我应该用哪一个 例如,我相信我在某个地方读到一些浏览器拆分时出现问题分号 学校: function getCookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(c_name + "="); if (c_start != -1) {

我发现有两个函数可以使用Javascript获取cookie数据,一个在上,另一个在
我想知道我应该用哪一个

例如,我相信我在某个地方读到一些浏览器拆分
时出现问题分号

学校:

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
怪癖模式:

function readCokie(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;
}
函数readCokie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
来自w3schools的这一点不正确,因为它可能导致获取错误的cookie:

c_start = document.cookie.indexOf(c_name + "=");
如果您要查找名为
foo
(我们假设它是一个现有的cookie)的cookie,那么
文档中的某个地方就会出现字符串
foo=bar

但是,不能保证也不会是字符串
xfoo=something
。请注意,它仍然包含子字符串
foo=
,因此代码将找到它。如果
xfoo
cookie恰好列在第一位,您将返回
something
值(错误!),而不是预期的


如果要在两段代码之间做出选择,千万不要选择根本上已损坏的代码。

W3CSchool的函数是错误的。如果有多个cookie具有相同的后缀,则失败,如:

ffoo=bar; foo=baz
搜索
foo
时,它将返回ffoo的值,而不是foo

现在我要做的是:首先,您需要了解Cookie如何传输的语法。Netscape的原始规范(只有副本可用,如)使用分号分隔多个cookie,而每个名称/值对具有以下语法:

名称
=

此字符串是一个字符序列,不包括分号、逗号和空格。如果需要在名称或值中放置此类数据,建议使用一些编码方法,如URL样式
%XX
编码,但不定义或不需要编码

因此,在分号或逗号处拆分
document.cookie
字符串是一种可行的选择

除此之外,还规定Cookie由分号或逗号分隔:

虽然两者都允许,但逗号是首选的,因为它们是HTTP中列表项的默认分隔符

注意:为了向后兼容,Cookie头中的分隔符 到处都是分号(
)。服务器还应该接受逗号(
) 作为cookie值之间的分隔符,以实现将来的兼容性

此外,名称/值对还有一些进一步的限制,因为值也可以是引用字符串,如中所述:

因此,这两个cookie版本需要分别处理:

if (typeof String.prototype.trimLeft !== "function") {
    String.prototype.trimLeft = function() {
        return this.replace(/^\s+/, "");
    };
}
if (typeof String.prototype.trimRight !== "function") {
    String.prototype.trimRight = function() {
        return this.replace(/\s+$/, "");
    };
}
if (typeof Array.prototype.map !== "function") {
    Array.prototype.map = function(callback, thisArg) {
        for (var i=0, n=this.length, a=[]; i<n; i++) {
            if (i in this) a[i] = callback.call(thisArg, this[i]);
        }
        return a;
    };
}
function getCookies() {
    var c = document.cookie, v = 0, cookies = {};
    if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
        c = RegExp.$1;
        v = 1;
    }
    if (v === 0) {
        c.split(/[,;]/).map(function(cookie) {
            var parts = cookie.split(/=/, 2),
                name = decodeURIComponent(parts[0].trimLeft()),
                value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
            cookies[name] = value;
        });
    } else {
        c.match(/(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g).map(function($0, $1) {
            var name = $0,
                value = $1.charAt(0) === '"'
                          ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
                          : $1;
            cookies[name] = value;
        });
    }
    return cookies;
}
function getCookie(name) {
    return getCookies()[name];
}
if(typeof String.prototype.trimleeft!=“函数”){
String.prototype.trimleet=函数(){
返回此。替换(/^\s+/,“”);
};
}
if(typeof String.prototype.trimRight!=“函数”){
String.prototype.trimRight=函数(){
返回此。替换(/\s+$/,“”);
};
}
if(typeof Array.prototype.map!=“函数”){
Array.prototype.map=函数(回调,thisArg){
对于(var i=0,n=this.length,a=[];i 1?decodeURIComponent(parts[1].trimRight()):null;
cookies[名称]=值;
});
}否则{
c、 匹配(/(?:^^\s+)([!$%&'*+\-.0-9A-Z^`a-Z.]+=([!\$%&'*+\-.0-9A-Z^`a-Z.]*.“(?:[\x20-\x7E\x80\xFF]\\\[\x00-\x7F]*”)=\s*.[\s*[,]-\x700-\x7F])/g)映射(函数($0,$1){
变量名称=$0,
值=$1。字符(0)='“'
?$1.子字符串(1,-1)。替换(/\\\()/g,“$1”)
: $1;
cookies[名称]=值;
});
}
归还饼干;
}
函数getCookie(名称){
返回getCookies()[name];
}

是的,W3Schools解决方案不正确

对于那些喜欢它的人,这里有一个更简单的解决方案,它只需预先添加一个空格,这样对indexOf()的单个调用只会返回正确的cookie

function getCookie(c_name) {
    var c_value = " " + document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_value = null;
    }
    else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start,c_end));
    }
    return c_value;
}

上面显示的所有代码都已中断。两个常见问题是:(1)如果一个cookie名称是另一个cookie名称的正确后缀,则getcookie函数可能返回错误的值;(2)setcookie函数不保护cookie值,这意味着如果cookie值包括(例如)一个“;“那么所有cookie都已损坏,无法解析

TL;DR使用这个编写良好的库:
这是我的版本,它涵盖了引用值的边缘情况

函数getCookies(){ 常量REGEXP=/([\w\.]+)\s*=\s*(?:”((?:\\“|[^])*)”(?:[;,]|$)s*(?:[;,])/g; 让cookies={}; 让我们比赛; while((match=REGEXP.exec(document.cookie))!==null){ 让值=匹配[2]| |匹配[3]; cookies[匹配[1]]=解码组件(值); } 归还饼干; }
他们都去寻找
字符来分割Cookie,据我所知这总是正确的。
getCookie
函数是错误的,想想如果你得到一个名为
ART=
的Cookie,而你有一个名为
SMART=
的Cookie,那么会发生什么情况。W3学校已经更改了getCookie函数(请参阅),现在可能更好了。他们现在使用相同的函数-只是名称和空格不同。我将
trimRight
trimLeft
移出
getCookie
函数:只需设置一次。检查$Version的代码部分是什么?这是我第一次在getCookie中看到它actions.it仍然有用吗?@Marco Demaio:RFC规范中的
$Version=1
属性用于标识这些规范和mi的cookie
if (typeof String.prototype.trimLeft !== "function") {
    String.prototype.trimLeft = function() {
        return this.replace(/^\s+/, "");
    };
}
if (typeof String.prototype.trimRight !== "function") {
    String.prototype.trimRight = function() {
        return this.replace(/\s+$/, "");
    };
}
if (typeof Array.prototype.map !== "function") {
    Array.prototype.map = function(callback, thisArg) {
        for (var i=0, n=this.length, a=[]; i<n; i++) {
            if (i in this) a[i] = callback.call(thisArg, this[i]);
        }
        return a;
    };
}
function getCookies() {
    var c = document.cookie, v = 0, cookies = {};
    if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
        c = RegExp.$1;
        v = 1;
    }
    if (v === 0) {
        c.split(/[,;]/).map(function(cookie) {
            var parts = cookie.split(/=/, 2),
                name = decodeURIComponent(parts[0].trimLeft()),
                value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
            cookies[name] = value;
        });
    } else {
        c.match(/(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g).map(function($0, $1) {
            var name = $0,
                value = $1.charAt(0) === '"'
                          ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
                          : $1;
            cookies[name] = value;
        });
    }
    return cookies;
}
function getCookie(name) {
    return getCookies()[name];
}
function getCookie(c_name) {
    var c_value = " " + document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_value = null;
    }
    else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start,c_end));
    }
    return c_value;
}