简体   繁体   中英

check cookie is set or not in javascript

I want to check cookie exist or not and then create a cookie in javascript and trigger a colorbox.inside the color box need to load the div which is created in php.now color is not triggered.

php

if(!isset($_COOKIE['first_time'])) {
    echo '<div class="celebration-popup"><img src="'.  base_path() . $directory.'/img/birthdayTemplate.jpg">/div>'; 
}

javascript

if (document.cookie.indexOf("first_time=") == 0){
    expiry = new Date();
    expiry.setTime(expiry.getTime()+(0.5*60*1000)); // 30 seconds
    document.cookie = "first_time=yes; expires=" + expiry.toGMTString();
    jQuery.colorbox({
        inline: true,
        href: ".celebration-popup",
        onClosed: function() {
          jQuery(".celebration-popup").hide()
        }
    });
} 

Something along these lines will work your php is relatively ok for working purposes, i think what was causing you an issue was your jquery calls.

if(!document.cookie.indexOf('fist_time'))
{
  var expiry = new Date();
  expiry.setTime(expiry.getTime() + 30000)
  document.cookie = 'first_time=yes; expires=' + expiry.toGMTString();
  $('.celebration-popup').colorbox({
    inline: true,
    href: ".celebration-popup",
    onClosed: function() 
    {
      $(".celebration-popup").hide()
    }
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM