当前位置: 移动技术网 > IT编程>开发语言>JavaScript > bootstrap datetimepicker 日期插件在火狐下出现一条报错信息的原因分析及解决办法

bootstrap datetimepicker 日期插件在火狐下出现一条报错信息的原因分析及解决办法

2019年06月13日  | 移动技术网IT编程  | 我要评论

日期插件 bootstrap-datetimepicker 在火狐下出现一条报错信息:typeerror: (intermediate value).tostring(…).split(…)[1] is undefined

这条错误必然出现,难道没有在 firefox 下进行测试。

在 firefox 下查看项目 demo ()可以正常运行,但这个 demo.php 使用的是 2013-3-2 的 datetimepicker,github 项目()已经发布到 2017-3-3,这个最新的版本(以及最近的一些版本)在 firefox 下测试不完善,计算 defaulttimezone 时虽然没有出错,但给出的结果也不正确。

源代码如下,运行环境 firefox 51.0.1(32位)

this.defaulttimezone = (new date).tostring().split('(')[1].slice(0, -1);
this.timezone = options.timezone || this.defaulttimezone;
// 2.4.4 改进版本
this.timezone = options.timezone || timezoneabbreviation();
function timezoneabbreviation() {
  var abbreviation, date, formattedstr, i, len, matchedstrings, ref, str;
  date = (new date()).tostring();
  formattedstr = ((ref = date.split('(')[1]) != null ? ref.slice(0, -1) : 0) || date.split(' ');
  if (formattedstr instanceof array) {
    matchedstrings = [];
    for (var i = 0, len = formattedstr.length; i < len; i++) {
      str = formattedstr[i];
      if ((abbreviation = (ref = str.match(/\b[a-z]+\b/)) !== null) ? ref[0] : 0) {
        matchedstrings.push(abbreviation);
      }
    }
    formattedstr = matchedstrings.pop();
  }
  return formattedstr;
}

出错原因是 firefox 下 date.prototype.tostring 返回结果不包含 timezone 的文字描述。

2.4.4 改进版本使用的 timezoneabbreviation 函数在 firefox 下返回  true

对 timezoneabbreviation 使用的三元表达式依次简化

((abbreviation = (ref = str.match(/\b[a-z]+\b/)) !== null) ? ref[0] : 0)
(abbreviation = (ref = str.match(/\b[a-z]+\b/)) !== null)
(abbreviation = (xxx) !== null)
(abbreviation = xxx !== null)
abbreviation 必然是布尔值,如果将 matchedstrings.push(abbreviation) 换成 matchedstrings.push(str) 更接近预期值。

推荐使用文末的方案。

解决方案

将 date tostring 最后一个空格之后的字符串作为 timezone。

// this.defaulttimezone = (new date).tostring().split('(')[1].slice(0, -1);
this.defaulttimezone = (new date + '').split(' ').slice(-1)[0].replace(/\(|\)/g, '');
this.timezone = options.timezone || this.defaulttimezone;

以上所述是小编给大家介绍的 bootstrap datetimepicker 日期插件在火狐下出现一条报错信息的原因分析及解决办法,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网