【问题标题】:How do I check if a cookie exists?如何检查 cookie 是否存在?
【发布时间】:2011-08-23 12:23:27
【问题描述】:

检查 cookie 是否存在的好方法是什么?

条件:

Cookie 存在,如果

cookie1=;cookie1=345534;
//or
cookie1=345534;cookie1=;
//or
cookie1=345534;

Cookie 不存在如果

cookie=;
//or
<blank>

【问题讨论】:

    标签: javascript html cookies


    【解决方案1】:

    你可以用你想要的cookie的名字调用getCookie函数,然后检查是否为=null。

    function getCookie(name) {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        }
        else
        {
            begin += 2;
            var end = document.cookie.indexOf(";", begin);
            if (end == -1) {
            end = dc.length;
            }
        }
        // because unescape has been deprecated, replaced with decodeURI
        //return unescape(dc.substring(begin + prefix.length, end));
        return decodeURI(dc.substring(begin + prefix.length, end));
    } 
    
    function doSomething() {
        var myCookie = getCookie("MyCookie");
    
        if (myCookie == null) {
            // do cookie doesn't exist stuff;
        }
        else {
            // do cookie exists stuff
        }
    }
    

    【讨论】:

    • 既然unescape已被弃用,那么改用decodeURIComponent有什么不同吗?
    • @the_nuts,不错的收获。我不知道这个。根据 w3cschools decodeURI(),或者 decodeURIComponent 可以用来代替 unescape。使用哪个选项可能取决于存储的内容,我选择了 decodeURI,因为我不希望 cookie 中有编码的分隔符。完整参考:w3schools.com/jsref/jsref_decodeuri.asp
    • 这不适用于第一个 cookie,因为未设置变量 end
    • 函数 doSomething($name) { var myCookie = getCookie($name);
    • 这是一个完美的例子,说明为什么 w3schools 不是可靠的来源,以及为什么您不应该从中复制/粘贴答案。你应该避免使用 == 和 != 这在 js 中是众所周知的。
    【解决方案2】:

    如果你使用 jQuery,你可以使用jquery.cookie plugin

    获取特定 cookie 的值如下:

    $.cookie('MyCookie'); // Returns the cookie value
    

    【讨论】:

    • 这也假设 OP 正在使用 jquery-cookie 插件。自从我使用 jquery 后,它让我陷入了一个循环,但无法使用该插件来完成我正在解决的任务。
    • 这不仅是 jquery 特有的,而且还需要一个 jquery 插件,您在答案中没有引用
    • 我真的很惊讶 2021 年 Javascript 并没有提供一种简单的方法来测试这样的 cookie,您可以在其中添加 cookie 的名称作为参数的内置方法。 JS 有时是多么奇怪和愚蠢的语言。
    【解决方案3】:

    我制作了一个替代的非 jQuery 版本:

    document.cookie.match(/^(.*;)?\s*MyCookie\s*=\s*[^;]+(.*)?$/)
    

    它只测试 cookie 是否存在。更复杂的版本也可以返回 cookie 值:

    value_or_null = (document.cookie.match(/^(?:.*;)?\s*MyCookie\s*=\s*([^;]+)(?:.*)?$/)||[,null])[1]
    

    用您的 cookie 名称代替 MyCookie

    【讨论】:

    • 绝妙的清洁解决方案!这些天人们抓住插件的速度太快了......太多的开销。这是一个非常好的解决方案!
    • 这不起作用。正则表达式缺少一个空格。应该是document.cookie.match(/^(.*;)?MyCookie=[^;]+(.*)?$/),注意?后面的空格。
    • document.cookie 返回以空格分隔的 cookie,即: cookie1= ; cookie1=345534;
    • @BogdanM。 : 第一个 cookie 没有空格! /[ ]{0,1}/ 见我上面的评论
    • 如果要使用变量:new RegExp("^(?:.*;)?\\s*" + cookieName + "\\s*=\\s*([^ ;]+)(?:.*)?$")
    【解决方案4】:
    document.cookie.indexOf('cookie_name=');
    

    如果该 cookie 不存在,它将返回 -1

    附言唯一的缺点是(如 cmets 中所述)如果有这样名称的 cookie 设置会出错:any_prefix_cookie_name

    (Source)

    【讨论】:

    • 这也匹配任何名称中带有字符串的cookie。例如。如果设置了cookie_name_whatever,则该示例返回-1 以外的其他内容(即使没有设置cookie_name)。另一个答案中的正则表达式版本解决了这个问题。
    • 虽然不是 100% 准确,但在大多数情况下这是一个足够好的解决方案。谢谢你 - 我觉得使用它比使用大型函数或其他解决方案中的复杂正则表达式更舒服。
    • @hajamie 我认为使用document.cookie.indexOf('cookie_name=') == 0; 可以解决这个问题。仅当 cookie 以提到的文本开头时才为真,否则为假。
    • @Lasse,仅当这是您拥有的唯一 cookie(或者它是第一个)时。 document.cookie 包含一个以分号分隔的所有 cookie 列表。
    【解决方案5】:

    regexObject.test( String ) 是 faster 而不是 string.match( RegExp )。

    MDN site 描述了 document.cookie 的格式,并有一个示例正则表达式来获取 cookie (document.cookie.replace(/(?:(?:^|.*;\s*)test2\s*\=\s*([^;]*).*$)|^.*$/, "$1");)。基于此,我会这样做:

    /^(.*;)?\s*cookie1\s*=/.test(document.cookie);
    

    这个问题似乎要求一个解决方案,在设置 cookie 时返回 false,但为空。在这种情况下:

    /^(.*;)?\s*cookie1\s*=\s*[^;]/.test(document.cookie);
    

    测试

    function cookieExists(input) {return /^(.*;)?\s*cookie1\s*=/.test(input);}
    function cookieExistsAndNotBlank(input) {return /^(.*;)?\s*cookie1\s*=\s*[^;]/.test(input);}
    var testCases = ['cookie1=;cookie1=345534;', 'cookie1=345534;cookie1=;', 'cookie1=345534;', ' cookie1 = 345534; ', 'cookie1=;', 'cookie123=345534;', 'cookie=345534;', ''];
    console.table(testCases.map(function(s){return {'Test String': s, 'cookieExists': cookieExists(s), 'cookieExistsAndNotBlank': cookieExistsAndNotBlank(s)}}));
    

    【讨论】:

    • 如何将变量作为 cookieName 传递,而不是将 cookieName 作为字符串传递?
    • var name = 'cookie1'; new RegExp('^(.*;)?\\s*' + name + '\\s*=').test(document.cookie);
    【解决方案6】:
    function getCookie(name) {
    
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
            else{
                var oneCookie = dc.indexOf(';', begin);
                if(oneCookie == -1){
                    var end = dc.length;
                }else{
                    var end = oneCookie;
                }
                return dc.substring(begin, end).replace(prefix,'');
            } 
    
        }
        else
        {
            begin += 2;
            var end = document.cookie.indexOf(";", begin);
            if (end == -1) {
                end = dc.length;
            }
            var fixed = dc.substring(begin, end).replace(prefix,'');
        }
        // return decodeURI(dc.substring(begin + prefix.length, end));
        return fixed;
    } 
    

    尝试了@jac 函数,遇到了一些麻烦,这是我编辑他的函数的方法。

    【讨论】:

      【解决方案7】:

      注意! 选择的答案包含错误(Jac's answer)

      如果您有多个 cookie(很可能..)并且您正在检索的 cookie 是列表中的第一个,它不会设置变量“end”,因此它将返回后面的整个字符串document.cookie 字符串中的“cookieName=”!

      这是该函数的修订版:

      function getCookie( name ) {
          var dc,
              prefix,
              begin,
              end;
          
          dc = document.cookie;
          prefix = name + "=";
          begin = dc.indexOf("; " + prefix);
          end = dc.length; // default to end of the string
      
          // found, and not in first position
          if (begin !== -1) {
              // exclude the "; "
              begin += 2;
          } else {
              //see if cookie is in first position
              begin = dc.indexOf(prefix);
              // not found at all or found as a portion of another cookie name
              if (begin === -1 || begin !== 0 ) return null;
          } 
      
          // if we find a ";" somewhere after the prefix position then "end" is that position,
          // otherwise it defaults to the end of the string
          if (dc.indexOf(";", begin) !== -1) {
              end = dc.indexOf(";", begin);
          }
      
          return decodeURI(dc.substring(begin + prefix.length, end) ).replace(/\"/g, ''); 
      }
      

      【讨论】:

      • 只有一组 cookie,您的函数返回“cookieName=”。 :-/
      • @Jeppe 我已经修改了代码,以便在只有一个 cookie 时也能正常工作。实际上,我已经借此机会重构了整个函数并对其进行了整理,并添加了一些 cmets。 ;)
      • replace(/"/g, '') 有问题;它给了我语法错误
      • 它对我有用,但可能最好避开正则表达式中的引号。我已经编辑了答案。现在应该也适合你!
      【解决方案8】:

      您可以使用 document.cookie.split...而不是 cookie 变量...

      var cookie = 'cookie1=s; cookie1=; cookie2=test';
      var cookies = cookie.split('; ');
      cookies.forEach(function(c){
        if(c.match(/cookie1=.+/))
         console.log(true);
      });

      【讨论】:

        【解决方案9】:

        对于任何使用 Node 的人,我发现了一个很好的简单解决方案,其中包含 ES6 导入和 cookie 模块!

        首先安装cookie模块(并保存为依赖):

        npm install --save cookie
        

        然后导入使用:

        import cookie from 'cookie';
        let parsed = cookie.parse(document.cookie);
        if('cookie1' in parsed) 
            console.log(parsed.cookie1);
        

        【讨论】:

          【解决方案10】:

          改用这个方法:

          function getCookie(name) {
              var value = "; " + document.cookie;
              var parts = value.split("; " + name + "=");
              if (parts.length == 2) return parts.pop().split(";").shift();
              else return null;
          }
          
          function doSomething() {
              var myCookie = getCookie("MyCookie");
          
              if (myCookie == null) {
                  // do cookie doesn't exist stuff;
              }
              else {
                  // do cookie exists stuff
              }
          }
          

          【讨论】:

            【解决方案11】:

            这是一个老问题,但这是我使用的方法......

            function getCookie(name) {
                var match = document.cookie.match(RegExp('(?:^|;\\s*)' + name + '=([^;]*)')); 
                return match ? match[1] : null;
            }
            

            当 cookie 不存在或不包含请求的名称时,这将返回 null
            否则,返回(请求名称的)值。

            cookie 永远不应该没有价值——因为,平心而论,这样做有什么意义? ?
            如果不再需要它,最好一起摆脱它。

            function deleteCookie(name) {
                document.cookie = name +"=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
            }
            

            【讨论】:

            • 有一个没有值的 cookie 有一个完全正当的理由,那就是表明某物的存在。它将像布尔值一样使用:cookie 存在 => true,cookie 不存在 => false
            • 这是列表中最好的方法。
            • 为什么不为我们内置这样的功能? 26.3 年前的 Netscape 0.9b 有 cookie - 尽管所有 ECMA 版本都推出了,但在 2.5 年中获取和设置它们似乎并没有显着改善。
            • 您可以通过return match &amp;&amp; match[1]; 进一步简化getCookie 函数,因为当match 不是“肯定”条件时,这将返回null
            【解决方案12】:
            /// ************************************************ cookie_exists
            
            /// global entry point, export to global namespace
            
            /// <synopsis>
            ///   cookie_exists ( name );
            ///
            /// <summary>
            ///   determines if a cookie with name exists
            ///
            /// <param name="name">
            ///   string containing the name of the cookie to test for 
            //    existence
            ///
            /// <returns>
            ///   true, if the cookie exists; otherwise, false
            ///
            /// <example>
            ///   if ( cookie_exists ( name ) );
            ///     {
            ///     // do something with the existing cookie
            ///     }
            ///   else
            ///     {
            ///     // cookies does not exist, do something else 
            ///     }
            
            function cookie_exists ( name )
              {
              var exists = false;
            
              if ( document.cookie )
                {
                if ( document.cookie.length > 0 )
                  {
                                                // trim name
                  if ( ( name = name.replace ( /^\s*/, "" ).length > 0 ) )
                    {
                    var cookies = document.cookie.split ( ";" );
                    var name_with_equal = name + "=";
            
                    for ( var i = 0; ( i < cookies.length ); i++ )
                      {
                                                // trim cookie
                      var cookie = cookies [ i ].replace ( /^\s*/, "" );
            
                      if ( cookie.indexOf ( name_with_equal ) === 0 )
                        {
                        exists = true;
                        break;
                        }
                      }
                    }
                  }
                }
            
              return ( exists );
            
              } // cookie_exists
            

            【讨论】:

              【解决方案13】:

              使用 Javascript:

               function getCookie(name) {
                    let matches = document.cookie.match(new RegExp(
                      "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
                    ));
                    return matches ? decodeURIComponent(matches[1]) : undefined;
                  }
              

              【讨论】:

                【解决方案14】:
                function getcookie(name = '') {
                    let cookies = document.cookie;
                    let cookiestore = {};
                    
                    cookies = cookies.split(";");
                    
                    if (cookies[0] == "" && cookies[0][0] == undefined) {
                        return undefined;
                    }
                    
                    cookies.forEach(function(cookie) {
                        cookie = cookie.split(/=(.+)/);
                        if (cookie[0].substr(0, 1) == ' ') {
                            cookie[0] = cookie[0].substr(1);
                        }
                        cookiestore[cookie[0]] = cookie[1];
                    });
                    
                    return (name !== '' ? cookiestore[name] : cookiestore);
                }
                

                要获取 cookie 对象,只需调用 getCookie()

                要检查 cookie 是否存在,请这样做:

                if (!getcookie('myCookie')) {
                    console.log('myCookie does not exist.');
                } else {
                    console.log('myCookie value is ' + getcookie('myCookie'));
                }
                

                或者只使用三元运算符。

                【讨论】:

                  【解决方案15】:

                  这里有几个很好的答案。然而,我更喜欢 [1] 不使用正则表达式,[2] 使用易于阅读的逻辑,而 [3]如果 name 是另一个 cookie name 的子字符串,则有一个简短的函数 [4] not 返回 true 。最后[5]我们不能使用 for each 循环,因为 return 不会破坏它。

                  function cookieExists(name) {
                    var cks = document.cookie.split(';');
                    for(i = 0; i < cks.length; i++)
                      if (cks[i].split('=')[0].trim() == name) return true;
                  }
                  

                  【讨论】:

                    【解决方案16】:
                    function hasCookie(cookieName){
                    return document.cookie.split(';')
                    .map(entry => entry.split('='))
                    .some(([name, value]) => (name.trim() === cookieName) && !!value);
                    }
                    

                    注意:作者希望函数在 cookie 为空时返回 false,即 cookie=;,这是通过 &amp;&amp; !!value 条件实现的。如果您认为空 cookie 仍然是现有 cookie,请将其删除...

                    【讨论】:

                      【解决方案17】:

                      var cookie = 'cookie1=s; cookie1=; cookie2=test';
                      var cookies = cookie.split('; ');
                      cookies.forEach(function(c){
                        if(c.match(/cookie1=.+/))
                         console.log(true);
                      });

                      【讨论】:

                      • 您好,欢迎来到 SO!虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。请阅读tourHow do I write a good answer?
                      【解决方案18】:

                      请注意,如果 cookie 是安全的,则您无法使用document.cookie(所有答案都在使用)在客户端检查它的存在。此类 cookie 只能在服务器端进行检查。

                      【讨论】:

                      • 我认为您的意思是 http only cookie。客户端仍然可以访问安全 cookie。
                      【解决方案19】:

                      您可以验证 cookie 是否存在以及它是否具有定义的值:

                      function getCookie(cookiename) {
                          if (typeof(cookiename) == 'string' && cookiename != '') {
                              const COOKIES = document.cookie.split(';');
                              for (i = 0; i < COOKIES.length; i++) {
                                  if (COOKIES[i].trim().startsWith(cookiename)) {
                                      return COOKIES[i].split('=')[1];
                                  }
                              }
                          }
                      
                          return null;
                      }
                      
                      const COOKIE_EXAMPLE = getCookie('example');
                      if (COOKIE_EXAMPLE == 'stackoverflow') { ... }
                      // If is set a cookie named "example" with value "stackoverflow"
                      if (COOKIE_EXAMPLE != null) { ... }
                      // If is set a cookie named "example" ignoring the value
                      

                      如果 cookie 不存在,它将返回 null。

                      【讨论】:

                        【解决方案20】:

                        将带有 Array.prototype.reduce() 的 cookie 解析为对象 (ES6)

                        const cookies = document.cookie.split(";").reduce((e, t) => {
                          const [c, n] = t.trim().split("=").map(decodeURIComponent);
                          try { // this can be removed if you do not need JSON cookies parsed
                            return Object.assign(e, {
                              [c]: JSON.parse(n)
                            })
                          }
                          catch (t) {
                            return Object.assign(e, {
                              [c]: n
                            })
                          }
                        }, {})
                        

                        检查您的 cookie 是否存在

                        typeof cookies.yourCookie === "string";
                        

                        【讨论】:

                          【解决方案21】:

                          如果有人仍在查看这篇文章,也许这会有所帮助。

                          首先做一个函数来获取cookie,像这样..

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

                          然后你可以在做其他事情之前检查特定的cookie是否存在

                          if( getCookie(mycookieName)){
                           // do something....
                          }
                          

                          【讨论】:

                            猜你喜欢
                            • 2012-10-15
                            • 2012-05-17
                            • 1970-01-01
                            • 2014-10-15
                            • 1970-01-01
                            • 2023-03-12
                            • 2012-03-10
                            • 2014-12-04
                            • 2020-11-11
                            相关资源
                            最近更新 更多