创建一个cookie if(且仅当它不存在) [英] Create a cookie if (and only if) it doesn't already exist

查看:193
本文介绍了创建一个cookie if(且仅当它不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要


  1. 检查是否存在名称为query / li>
  2. 如果是,则不执行任何操作

  3. 如果否,请创建值为1的Cookiequery / ol>

    注意:我使用的是jQuery 1.4.2和 jQuery cookie plugin



    有没有人对我有什么建议?

    解决方案

      if($。cookie('query')=== null){
    $ .cookie('query',' 1',{expires:7,path:'/'});
    }

    或者,您可以为此写一个包装函数:

      jQuery.lazyCookie = function(){
    if(jQuery.cookie(arguments [0])!== null)return;
    jQuery.cookie.apply(this,arguments);
    };

    然后,您只需要在客户端代码中写下:

      $。lazyCookie('query','1',{expires:7,path:'/'}); 


    I want to:

    1. Check to see if a cookie with name of "query" exists
    2. If yes, then do nothing
    3. If no, create a cookie "query" with a value of 1

    Note: I am using jQuery 1.4.2 and the jQuery cookie plugin.

    Does anyone have any suggestions as to how I can do this?

    解决方案

    if($.cookie('query') === null) { 
        $.cookie('query', '1', {expires:7, path:'/'});
    }
    

    Alternatively, you could write a wrapper function for this:

    jQuery.lazyCookie = function() {
       if(jQuery.cookie(arguments[0]) !== null) return;
       jQuery.cookie.apply(this, arguments);
    };
    

    Then you'd only need to write this in your client code:

    $.lazyCookie('query', '1', {expires:7, path:'/'});
    

    这篇关于创建一个cookie if(且仅当它不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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