分类: 前端

  • 在 Firefox 下 bootstrap-datetimepicker 报错

    日期插件 bootstrap-datetimepicker 在火狐下出现一条报错

    TypeError: (intermediate value).toString(…).split(…)[1] is undefined

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

    在 Firefox 下查看项目 demo (http://www.malot.fr/bootstrap-datetimepicker/demo.php)可以正常运行,但这个 demo.php 使用的是 2013-3-2 的 datetimepicker,github 项目(https://github.com/smalot/bootstrap-datetimepicker/releases)已经发布到 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) 更接近预期值。

    推荐使用文末的方案。


    • Firefox date toString 返回结果没有(中国标准时间)“, 因此 split('(')[1] 是 undefined

    • Chrome date toString 返回结果包含 “(中国标准时间)

    • IE/Edge date toString 返回结果包含 “(中国标准时间)

    解决方案

    将 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;
    
  • CSS 压缩 initial、normal

    CSS 压缩在完成简单的字符替换,空白符压缩之后,还可以进一步进行特定 value 压缩。

    比如:

    /* 压缩前 */
    .main{margin:initial}
    
    /* 压缩后 */
    .main{margin:0px}
    
    /* 进一步压缩 */
    .main{margin:0}
    

    最后只需要占用一个字符就达到 initial 效果。

    css initial 是一个特殊值,用于设置一些复杂、容易混淆的、难记忆的初始值非常有效。

    /* 压缩前 */
    .main{background:initial}
    
    /* 压缩后 */
    .main{background:rgba(0, 0, 0, 0) none repeat scroll 0% 0% / auto padding-box border-box}
    

    对于这种真实样式比 initial 更长的样式可以不用压缩。

    Chrome 下 css initial 对应真实值

    在多个浏览器遍历设置元素 css 样式为 initial,再通过 getComputedStyle 获取真实样式,将其中相同的部分归纳到 initial 集合中,提供给压缩脚本使用,即可完成 css 特定 value 压缩。

    同样可以压缩 css normal,使文件更小。

  • Closure-Compiler 指定 charset 减少输出文件大小

    Google 的 Closure-Compiler  压缩 JavaScript 文件默认采用 UTF-8 作为输入编码,US_ASCII 作为输出编码。

    –charset VAL : Input and output charset for all files
    . By default, we accept UTF-8 as input
    and output US_ASCII

    因此,压缩汉字(或者日韩文字)之后,文件会变大。

    执行命令 java -jar $dir/compiler.jar --js $cache_file --js_output_file $output_file

    // 输入文件
    var address = '上海';
    
    // 输出文件
    var address="\u4e0a\u6d77";
    

    源文件较小时可以忽略这个问题,但源文件有大量汉字,可以指定 charset 避免输出文件过大。

    执行命令 java -jar $dir/compiler.jar --js $cache_file --js_output_file $output_file --charset=UTF-8

    // 输入文件
    var address = '上海';
    
    // 输出文件
    var address="上海";
    

    在开发中遇到一个 1.5MB 的地址文件,压缩之后变成 3MB,再经 gzip 压缩变成 780 KB 左右。而源文件经 gzip 之后在 590 KB。

  • win10升级后字体异常解决方案

    今天 win10 自动更新之后,chrome 下使用了微软雅黑的网页字体看着非常难受,不论如何修改 font-family,就是无法显示正常的微软雅黑。

    想起刚启动 chrome 时,发现默认浏览器不是 chrome,而是 edge。尝试用 edge 打开网页,显示正常。猜想不是字体的原因。

    百度之后,找到是 chrome 默认开启 DirectWrite 渲染。

    用 chrome 在地址栏打开 chrome://flags/ ,停用 DirectWrite 即可显示正常。

    对比一下开启和停用效果图

    开启效果图(默认状态)

    停用效果图

  • JavaScript 获取两个字符串最长公共子串 (Longest Common Subsequence)

    获取两个字符串 a 和 b 的最长公共子串,时间复杂度 O(mn),空间复杂度 O(n),其中 a 的字符串长度为 m,b 的字符串长度为 n。

    function lcs(a, b) {
        var ai, al = a.length,
            bi, bl = b.length - 1, maxBi,
            max = 0, val,
            row = [],
            ret = [];
    
        for (ai = 0; ai < al; ai++) {
            for (bi = bl; bi > -1; bi--) {
                val = a[ai] == b[bi] ? 1 : 0;
                if (val) {
                    if (row[bi - 1]) {
                        val += row[bi - 1];
                    }
                    if (val > max) {
                        max = val;
                        maxBi = bi;
                    }
                }
                row[bi] = val;
            }
        }
    
        for (; max > 0; max--, maxBi--) {
            ret.unshift(b[maxBi]);
        }
    
        return ret.join('');
    }
    
  • 详细分析 setInterval 和 setTimeout 区别

    理解两者区别需要先知道:

    1. JavaScript 是单线程
    2. setInterval 是周期性地调用一个函数(一段可执行代码)
    3. setTimeout 是在一定延迟之后调用一个函数(一段可执行代码)
    function cost150ms(){
        // 该函数执行 150ms
    }
    function testTimeout() {
        // 函数在 150ms 之后执行
    }
    function testInterval() {
        // 函数在 150ms、200ms、300ms、400ms... 执行
    }
    
    setTimeout(testTimeout, 100);
    setInterval(testInterval, 100);
    cost150ms();
    

    结论

    setTimeout(callable, 100);
    otherCode();
    

    callable 是周期性执行,不管 otherCode 执行多长时间。
    如果到达下一个周期,otherCode 仍然在执行,则 callable 被推迟到 otherCode 执行完毕,即产生“跳过”。
    所以上面的例子中能看到 callable 第一次执行是 150ms,第二次执行时 200ms,两次执行只相差 50ms。
    假如在某个周期 otherCode 仍在在执行,则该周期 callable 继续被推迟到 otherCode 执行完毕。

    setTimeout(callable, 100);
    otherCode();
    

    callable 调用时间是 otherCode 执行时间与 100ms 最大值。

    setInterval 和 setTimeout 都有可能出现推迟执行,setInterval 由于是周期性执行,表现上更像是“跳过”,setTimeout 更像是推迟。

    实际场景不会是刚刚等于 150ms、200ms,但接近该值。

  • 单字母标签列表

    HTML 定义的一个字母标签列表

    标签 说明
    a 定义超链接
    b 定义粗体文本
    i 定义斜体文本
    p 定义段落
    q 定义简短的行内引用,区别于 <blockquote> 定义块引用
    s 定义删除线文本,是 <strike> 的缩写,但在 HTML 4.01 不赞成使用这两个标签,推荐使用 <del> 替代,目前浏览器仍然支持
    u 定义下划线文本,在 HTML 4.01 不赞成使用,目前浏览器仍然支持
  • 修改WordPress标题分隔符

    我的博客升级到 4.5.1 之后,网页标题的分隔符从中文竖线“|”变成了“-”,刚开始看着不太习惯,而且发现源代码中“–”被转义成了实体“&#8211;”,这样看着更不舒服。

    于是尝试修改WordPress网页标题分隔符。

    刚开始仍然想使用“|”作为分隔符,后来决定参考网页标题分隔符采取哪一种比较好。

    关于标题分隔符的建议:http://jingyan.baidu.com/article/bad08e1eae3fbb09c851213c.html

    发现腾讯和网易都采用“_”作为标题分隔符,于是也计划改成“_”。

    WordPress似乎没有提供配置修改网页标题分隔符。

    修改方法

    修改文件 wp-includes/general-template.php

    // 第 1022 行
    // 修改前
    $sep = apply_filters( 'document_title_separator', '-' );
    // 修改后
    $sep = apply_filters( 'document_title_separator', '_' );
    
    // 第 1038 行
    // 修改前
    $title = implode( " $sep ", array_filter( $title ) );
    // 修改后
    $title = implode( "$sep", array_filter( $title ) );
    
  • 网页加载优化 DNS 预解析

    网站功能越来越复杂,除了主站域名、CDN域名、图片域名、接口域名,还可能有第三方分享、OAuth认证。

    <!-- 1, 通过设置 header 或者 meta 标签
        设置 x-dns-prefetch-control 为 on -->
    <meta http-equiv="x-dns-prefetch-control" content="on">
    <!-- 2, 通过 link 标签指定要预解析的域名 -->
    <link rel="dns-prefetch" href="//s.zhengxianjun.com">
    

    附:DNS解析过程

  • 对比jQuery和Zepto

    对比两者到2016年3月8号最新的版本 jQuery 2.2.1 http://jquery.com/ 和 Zepto 1.1.6 http://zeptojs.com/

    简单对比

    jQuery 2.2.1 Zepto 1.1.6
    文件大小
    浏览器支持
  • Internet Explorer 9+
  • Chrome, Edge, Firefox (Current – 1) or Current
  • Safari 5.1+
  • Opera 12.1x, (Current – 1) or Current
  • iOS 6.1+
  • Android 2.3, 4.0+
  • Primary (100% support)

  • Safari 6+ (Mac)
  • Chrome 30+ (Windows, Mac, Android, iOS, Linux, Chrome OS)
  • Firefox 24+ (Windows, Mac, Android, Linux, Firefox OS)
  • iOS 5+ Safari
  • Android 2.3+ Browser
  • Internet Explorer 10+ (Windows, Windows Phone)
  • Secondary targets (fmenuly or mostly supported)

  • iOS 3+ Safari
  • Chrome <30
  • Firefox 4+
  • Safari <6
  • Android Browser 2.2
  • Opera 10+
  • webOS 1.4.5+ Browser
  • BlackBerry Tablet OS 1.0.7+ Browser
  • Amazon Silk 1.0+
  • Other WebKit-based browsers/runtimes
  • 成员函数数量
    工具函数数量
    授权协议 无特殊说明均为MIT MIT

    详细对比