jQuery check if Cookie exists, if not create it

后端 未结 5 640
春和景丽
春和景丽 2020-12-02 20:27

I cannot get this code to work I must be missing something pretty simple. I am trying to check to see if a Cookie exists, if it does {do nothing} if it doesn\'t {create it}

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 21:01

    You can set the cookie after having checked if it exists with a value.

     $(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
          }       
     });
    

提交回复
热议问题