参数列表后面的Js cookies:SyntaxError:missing)

时间:2016-08-29 09:38:54

标签: javascript cookies

当我设置一个带日期的cookie时会抛出错误:

var COOKIE = {

    setCookie: function(name, value, expdate) {
        'use strict';

        var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
        str +=';expires=' + expdate.toUTCString();
        alert("in");
        console.log(expdate.toUTCString());
        document.cookie = str;
    } // End of setCookie() function

};

window.onload = function() {
    'use strict';
   COOKIE.setCookie('username', 'John', Mon 29 Aug 2016 11:40:00 UTC ); // date
};

但如果我使用了很多天,它就没有了:

var COOKIE = {

    setCookie: function(name, value, numdays) {
        'use strict';

         var cookieDate = new Date();
       cookieDate.setDate(cookieDate.getDate() + numdays);

        var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
        str +=';expires=' + cookieDate.toGMTString();
        alert("in");
        console.log(cookieDate.toGMTString());
        document.cookie = str;
    } // End of setCookie() function

};

window.onload = function() {
    'use strict';
   COOKIE.setCookie('username', 'John', 1 ); // day
};

我尊重官方文件所期望的格式。这里发生了什么?谢谢,

1 个答案:

答案 0 :(得分:0)

您只需要关注调试器。它指向你的行:

COOKIE.setCookie('username', 'John', Mon 29 Aug 2016 11:40:00 UTC );

Mon 29 Aug 2016 11:40:00 UTC无效的JavaScript。也许它本来是用字符串作为引号。