在JavaScript中按名称获取cookie的最佳方法是什么?

时间:2021-10-01 02:47:36

I am using prototype and I can't find any built in extensions to set or retrieve cookies. After googling for a little bit, I see a few different ways to go about it. I was wondering what you think is the best approach for getting a cookie in JavaScript?

我正在使用原型,我找不到任何内置的扩展来设置或检索cookie。谷歌搜索了一下之后,我看到了几种不同的方法。我想知道你认为在JavaScript中获取cookie的最佳方法是什么?

4 个解决方案

#1


9  

I use this routine:

我用这个例程:

function ReadCookie(name)
{
  name += '=';
  var parts = document.cookie.split(/;\s*/);
  for (var i = 0; i < parts.length; i++)
  {
    var part = parts[i];
    if (part.indexOf(name) == 0)
      return part.substring(name.length)
  }
  return null;
}

Works quite well.

效果很好。

#2


2  

Anytime I need to access it, I use document.cookie, basically how it's outlined in that article. Caveat, I've never used prototype, so there may be easier methods there that you just haven't run across.

任何时候我需要访问它,我使用document.cookie,基本上是如何在该文章中概述。警告,我从来没有使用原型,因此可能有更简单的方法,你只是没有遇到过。

#3


2  

In case anyone else needs it, I've fixed up Diodeus's code to address PhiLho's concern about partial matches when trying to fetch a cookie value.

如果其他人需要它,我已经修复了Diodeus的代码,以解决PhiLho在尝试获取cookie值时对部分匹配的担忧。

function getCookie(c_name) {
    var nameEQ = c_name + '=';
    var c_start = 0;
    var c_end = 0;
    if (document.cookie.substr(0, nameEQ.length) === nameEQ) {
        return document.cookie.substring(nameEQ.length, document.cookie.indexOf(';', nameEQ.length));
    } else {
        c_start = document.cookie.indexOf('; ' + nameEQ);
        if(c_start !== -1){
            c_start += nameEQ.length + 2;
            c_end = document.cookie.indexOf(';', c_start);
            if (c_end === -1) {c_end = document.cookie.length;}
            return document.cookie.substring(c_start, c_end);
        }
    }
    return null;
}

I've recently also built a much more compact RegExp that should work as well:

我最近还构建了一个更紧凑的RegExp,它也可以运行:

function getCookie(c_name){
    var ret = window.testCookie.match(new RegExp("(?:^|;)\\s*"+c_name+"=([^;]*)"));
    return (ret !== null ? ret[1] : null);
}

I did some speed tests that seem to indicate that out of PhiLo, QuirksMode, and these two implementations the non-RegExp version (using indexOf is very fast, not a huge surprise) above is the fastest. http://jsperf.com/cookie-fetcher

我做了一些速度测试,似乎表明PhiLo,QuirksMode和这两个实现的非RegExp版本(使用indexOf非常快,不是一个巨大的惊喜)上面是最快的。 http://jsperf.com/cookie-fetcher

#4


-2  

I use this. It has been dependable:

我用这个。它是可靠的:

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 ""

}

#1


9  

I use this routine:

我用这个例程:

function ReadCookie(name)
{
  name += '=';
  var parts = document.cookie.split(/;\s*/);
  for (var i = 0; i < parts.length; i++)
  {
    var part = parts[i];
    if (part.indexOf(name) == 0)
      return part.substring(name.length)
  }
  return null;
}

Works quite well.

效果很好。

#2


2  

Anytime I need to access it, I use document.cookie, basically how it's outlined in that article. Caveat, I've never used prototype, so there may be easier methods there that you just haven't run across.

任何时候我需要访问它,我使用document.cookie,基本上是如何在该文章中概述。警告,我从来没有使用原型,因此可能有更简单的方法,你只是没有遇到过。

#3


2  

In case anyone else needs it, I've fixed up Diodeus's code to address PhiLho's concern about partial matches when trying to fetch a cookie value.

如果其他人需要它,我已经修复了Diodeus的代码,以解决PhiLho在尝试获取cookie值时对部分匹配的担忧。

function getCookie(c_name) {
    var nameEQ = c_name + '=';
    var c_start = 0;
    var c_end = 0;
    if (document.cookie.substr(0, nameEQ.length) === nameEQ) {
        return document.cookie.substring(nameEQ.length, document.cookie.indexOf(';', nameEQ.length));
    } else {
        c_start = document.cookie.indexOf('; ' + nameEQ);
        if(c_start !== -1){
            c_start += nameEQ.length + 2;
            c_end = document.cookie.indexOf(';', c_start);
            if (c_end === -1) {c_end = document.cookie.length;}
            return document.cookie.substring(c_start, c_end);
        }
    }
    return null;
}

I've recently also built a much more compact RegExp that should work as well:

我最近还构建了一个更紧凑的RegExp,它也可以运行:

function getCookie(c_name){
    var ret = window.testCookie.match(new RegExp("(?:^|;)\\s*"+c_name+"=([^;]*)"));
    return (ret !== null ? ret[1] : null);
}

I did some speed tests that seem to indicate that out of PhiLo, QuirksMode, and these two implementations the non-RegExp version (using indexOf is very fast, not a huge surprise) above is the fastest. http://jsperf.com/cookie-fetcher

我做了一些速度测试,似乎表明PhiLo,QuirksMode和这两个实现的非RegExp版本(使用indexOf非常快,不是一个巨大的惊喜)上面是最快的。 http://jsperf.com/cookie-fetcher

#4


-2  

I use this. It has been dependable:

我用这个。它是可靠的:

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 ""

}