在JavaScript中获取“未捕获的SyntaxError:无效或意外的令牌”

时间:2018-11-17 10:38:27

标签: javascript jquery arrays datetime datepicker

使用预订表格时,我的网站上出现此错误。

  

未捕获的SyntaxError:无效或意外的令牌       在Function.Date.createNewFormat(jquery.stmdatetimepicker.js?ver = 4.2.7:1857)       在Date.dateFormat(jquery.stmdatetimepicker.js?ver = 4.2.7:1857)       在XDSoft_datetime._this.str(jquery.stmdatetimepicker.js?ver = 4.2.7:1196)       在HTMLDivElement。 (jquery.stmdatetimepicker.js?ver = 4.2.7:1549)       在HTMLDivElement.dispatch(jquery.js?ver = 1.12.4:3)       在HTMLDivElement.r.handle(jquery.js?ver = 1.12.4:3)

这是链接:mantovacar.com/wp-content/themes/motors/assets/js/jquery.stmdatetimepicker.js?ver=4.2.7

1 个答案:

答案 0 :(得分:0)

我已经进行了调试,这就是您的问题: var dateTimeFormat = 'j F Y G \h i \m\i\n';

您可能希望至少将\n替换为n,但是整个格式对我来说似乎无效。

发生此问题的原因是该插件从格式中将代码创建为字符串,然后对其调用eval(),这显然是一种不好的做法。当您将\n作为格式的一部分传递时,它会转换为(换行符)。而且它破坏了代码,因为它在字符串中,如下所示:

Date.prototype.format2 = function() { ... + '
';}

我建议您使用此库的更新版本(也许他们已对其进行修复):https://xdsoft.net/jqplugins/datetimepicker/

并考虑切换到一些更常见的日期格式,例如Y/m/d H:i

我找不到有关插件格式的文档,因此您至少可以检查源以查看可以在dateTimeFormat中使用的所有可用字母:

Date.getFormatCode = function(a) {
    switch (a) {
    case "d":
        return "String.leftPad(this.getDate(), 2, '0') + ";
    case "D":
        return "Date.dayNames[this.getDay()].substring(0, 3) + ";
    case "j":
        return "this.getDate() + ";
    case "l":
        return "Date.dayNames[this.getDay()] + ";
    case "S":
        return "this.getSuffix() + ";
    case "w":
        return "this.getDay() + ";
    case "z":
        return "this.getDayOfYear() + ";
    case "W":
        return "this.getWeekOfYear() + ";
    case "F":
        return "Date.monthNames[this.getMonth()] + ";
    case "m":
        return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
    case "M":
        return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
    case "n":
        return "(this.getMonth() + 1) + ";
    case "t":
        return "this.getDaysInMonth() + ";
    case "L":
        return "(this.isLeapYear() ? 1 : 0) + ";
    case "Y":
        return "this.getFullYear() + ";
    case "y":
        return "('' + this.getFullYear()).substring(2, 4) + ";
    case "a":
        return "(this.getHours() < 12 ? 'am' : 'pm') + ";
    case "A":
        return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
    case "g":
        return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
    case "G":
        return "this.getHours() + ";
    case "h":
        return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
    case "H":
        return "String.leftPad(this.getHours(), 2, '0') + ";
    case "i":
        return "String.leftPad(this.getMinutes(), 2, '0') + ";
    case "s":
        return "String.leftPad(this.getSeconds(), 2, '0') + ";
    case "O":
        return "this.getGMTOffset() + ";
    case "T":
        return "this.getTimezone() + ";
    case "Z":
        return "(this.getTimezoneOffset() * -60) + ";
    default:
        return "'" + String.escape(a) + "' + ";
    }
}

另外,似乎您的js文件的编码存在一些问题,应该检查一下并切换到UTF-8。但这不在问题范围内。

现在,只需在索引页面上更改dateTimeFormat变量即可。