简体   繁体   中英

jQuery Cookie - update the value

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: '/' });
    }

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: '/' });
}

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