【问题标题】:jQuery check if Cookie exists, if not create itjQuery检查Cookie是否存在,如果不存在则创建它
【发布时间】:2011-09-15 19:15:17
【问题描述】:

我无法让这段代码工作,我一定是遗漏了一些非常简单的东西。我正在尝试检查 Cookie 是否存在,如果它没有 {create it},它是否 {do nothing}。我正在通过在页面上包含警报来测试 cookie。基本上我不希望 cookie 继续使用推荐 URL 重新创建,我试图只获取第一个引用的 URL。

$(document).ready(function(){   
  if ($.cookie('bas_referral') == null ){
   var ref = document.referrer.toLowerCase();  
   // set cookie  
   var cookURL =  $.cookie('bas_referral', ref, { expires: 1 }); 
  } 
 });  

显示当前 cookie 内容:

    // get cookie  
    alert($.cookie('bas_referral'));  

    // delete cookie  
     $.cookie('bas_referral', null);

【问题讨论】:

  • 似乎工作得很好:jsfiddle.net/niklasvh/wxFvG。您是否在页面中包含了$.cookie 插件源代码?
  • 您是通过 HTTP 打开页面吗? (因此不是来自本地磁盘文件系统)您使用的是什么 cookie 插件?过期值是什么时间单位?秒?那么,它会在 1 秒后过期吗?
  • 我正在使用 jquery.cookie.js 插件,我的警报显示我之前打开的 URL,所以我相信正在创建 cookie。但是,如果我要访问整个站点的不同页面,显示 cookie URL 的警报会发生变化,这不应该是我设置了 IF 语句。

标签: jquery cookies if-statement referrer isnull


【解决方案1】:

我认为防弹的方法是:

if (typeof $.cookie('token') === 'undefined'){
 //no cookie
} else {
 //have cookie
}

检查 null、空或未定义 var 的类型总是返回“未定义”

编辑: 您可以更轻松地到达那里:

if (!!$.cookie('token')) {
 // have cookie
} else {
 // no cookie
}

!! 会将falsy values 变为 false。请记住,这会将 0 变为 false!

【讨论】:

  • 警告:typeof null === 'object' as Dan cmets 下面。
  • 这是给定问题的正确答案,@foo!当我测试代码时,真假上下文反之亦然: if (!!$.cookie('token')) { // have cookie } else { // no cookie }
  • 哇!它检查空、空和未定义!以前不知道有更短的方法来检查这三个。不错
  • 如果cookie的值为false怎么办?可能更好地检查nullundefined
  • @foo 较短的答案对我不起作用。它与答案所说的相反 - 至少对我来说!
【解决方案2】:
 $(document).ready(function() {

     var CookieSet = $.cookie('cookietitle', 'yourvalue');

     if (CookieSet == null) {
          // Do Nothing
     }
     if (jQuery.cookie('cookietitle')) {
          // Reactions
     }
 });

【讨论】:

  • 我不知道为什么,但这是设置一个cookie,而不是检查是否存在,很奇怪!
  • var CookieSet = $.cookie('cookietitle', 'yourvalue');不能在我的电脑上运行,可能是什么原因?我正在使用 jquery 1.9.1 库。
  • 嗯,我找到了原因;我没有从 GitHub 安装插件,我意识到这是一个原生功能。阅读本文的人应该从(谢谢@Kazar)下载插件:github.com/carhartl/jquery-cookie
【解决方案3】:

我遇到了很多麻烦,因为我正在使用:

if($.cookie('token') === null || $.cookie('token') === "")
{
      //no cookie
}
else
{
     //have cookie
}

无论我在设置 cookie 方面做了什么,以上总是返回 false。从我的测试来看,似乎该对象在设置之前是未定义的,因此将以下内容添加到我的代码中修复了它。

if($.cookie('token') === null || $.cookie('token') === "" 
    || $.(cookie('token') === "null" || $.cookie('token') === undefined)
{
      //no cookie
}
else
{
     //have cookie
}

【讨论】:

    【解决方案4】:

    您可以在检查是否存在带有值的 cookie 后设置它。

     $(document).ready(function(){            
          if ($.cookie('cookie')) { //if cookie isset
             //do stuff here like hide a popup when cookie isset
             //document.getElementById("hideElement").style.display = "none";
          }else{
             var CookieSet = $.cookie('cookie', 'value'); //set cookie
          }       
     });
    

    【讨论】:

      【解决方案5】:

      试试这个很简单:

                  var cookieExist = $.cookie("status");
                  if(cookieExist == "null" ){
                      alert("Cookie Is Null");
      
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-22
        • 1970-01-01
        • 1970-01-01
        • 2014-07-23
        • 2011-05-12
        相关资源
        最近更新 更多