0

I am using jQuery cookie library. I need create a cookie and if the cookie exists to update the value. How can I do this?

   if ($.cookie('checkID') == null {
           var getHost = getHostname.hostname;

            switch (getHost) {
                case 'www.google.com':
                    var GetParamID = 'Abc1';
                    break;
                case 'www.yahoo.com':
                    var GetParamID = '345Cdv';
                    break;
             default:
                    var GetParamID = '';

      $.cookie('checkID', GetParamID , { path: '/' });
    }
dmi
  • 21
  • 2

1 Answers1

0

You're missing a ")" in the first line of code, and you should check for undefined (see here) as in jQuery Cookie documentation ( https://github.com/carhartl/jquery-cookie ). Other than that, your code should work. Be aware that if your cookie has been previously set in a different path than "/", you will have two different cookies (see: How to handle multiple cookies with the same name? )

if (typeof ($.cookie('checkID')) === "undefined") {
       var getHost = getHostname.hostname;

        switch (getHost) {
            case 'www.google.com':
                var GetParamID = 'Abc1';
                break;
            case 'www.yahoo.com':
                var GetParamID = '345Cdv';
                break;
         default:
                var GetParamID = '';

  $.cookie('checkID', GetParamID , { path: '/' });
}
Community
  • 1
  • 1
MarcoReni
  • 468
  • 5
  • 14