Entries in the ‘前端开发’ Category:

Script 元素的异步加载属性

进入正题之前,先考大家一个问题:defer 属性现在有哪些浏览器支持?

喜悦

除了 defer 属性,script 还新增了一个 async 属性,请看 MDC

async Requires Gecko 1.9.2
This Boolean attribute is set to indicate that the browser should, if possible, execute the script asynchronously. If the script cannot be executed asynchronously, or the browser doesn’t support this attribute, the script is executed synchronously (and loading the content blocks until the script finishes running).

理想情况下,利用 async 属性,脚本异步加载将变得非常简单:
async-test.js:

var g = 1;

async-test-1.html:

<script src="async-test.js" async="true"></script>
<script>
    alert(typeof g); // Firefox 3.6 弹出 undefined
</script>

测试页面:async-test-1.html
目前只有 Firefox 3.6 支持 async 属性,希望其它浏览器能迅速跟进。
阅读全文 »

Tags: , , ,

Chrome V8 引擎对 sort 的优化

var a = 0, b = 0;
[0, 0].sort(function() {
    a = 1;
    return 0;
});
[0, 1].sort(function() {
    b = 1;
    return 0;
});
alert(a === b); // true or false ?

上面的代码,除了 Chrome 输出 false, 其它浏览器皆为 true.

原因是 Chrome 对数组的 sort 方法进行了优化:

function sort(comparefn) {
  var custom_compare = (typeof(comparefn) === 'function');
  function Compare(x,y) {
    if (x === y) return 0;
    if (custom_compare) {
      return comparefn.call(null, x, y);
    }
    ...
}

虽然是优化,但也是陷阱。想用 sort 来干点额外体力活时,一定要小心。

Tags: , , ,

JavaScript 全半角转换

规律:半角空格的 charCode 为 32, 全角空格为 12288. 其他半角字符 ( 33 – 126 ) 与全角 ( 65281 – 65374 ) 的对应关系是:均相差 65248.

找好规律,代码就好写了:

var hash = {'32' : '\u3000'};
// 半角转全角
function sbc2dbc(str) {
    var ret = [], i = 0, len = str.length, code, chr;
    for (; i < len; ++i) {
        code = str.charCodeAt(i);
        chr = hash[code];
        if (!chr && code > 31 && code < 127) {
            chr = hash[code] = String.fromCharCode(code + 65248);
        }
        ret[i] = chr ? chr : str.charAt(i);
    }
    return ret.join('');
}

阅读全文 »

Tags: , , ,

Study jQuery in a Simplified Way

学习复杂代码的最好方法是简化:

(function(win, undefined) {
    var jQuery = function(selector, context) {
            // jQuery 对象就是 init 函数的一个实例
            return new jQuery.fn.init(selector, context);
        },
        document = window.document,
        push = Array.prototype.push,
        slice = Array.prototype.slice;

    jQuery.fn = jQuery.prototype = {
        init: function(selector, context) {
            // 选择器
            var ret = (context || document).querySelectorAll(selector);

            // 转换为普通数组
            ret = slice.call(ret);

            // jQuery API 的奥妙全在下面这句,将选择器获取的元素添加到 jQuery 对象中
            // 使用 push, 速度飞快(当年担心大量 jQuery 对象实例化的性能问题,根本就不是问题)
            // 使用 push, 还能自动更新 length 属性
            push.apply(this, ret);

            return this;
        },
        length: 0,
        // 实例方法
        attr: function(name, value) {
            return access(this, name, value, jQuery.attr);
        }
    };

    // 这句保证了 init 方法里的 this 拥有 jQuery 实例的方法
    jQuery.fn.init.prototype = jQuery.fn;

    // 静态方法
    jQuery.attr = function(elem, name, value) {
        if (value === undefined) {
            return elem.getAttribute(name);
        }
        return elem.setAttribute(name, value);
    };

    // 神奇的 access, 在实例方法和静态方法中建立了一座桥梁
    // 数组批次操作的实现也在这里
    function access(elems, key, value, fn) {
        var length = elems.length;

        if (value !== undefined) {
            for (var i = 0; i < length; i++) {
                fn(elems[i], key, value);
            }
            return elems;
        }

        return length ? fn(elems[0], key) : null;
    }

    win.$ = win.jQuery = jQuery;
})(window);

测试页面:study-jquery-in-simplified-way.html (请在非 IE 浏览器下运行)

源码:jquery~core.js

Tags: , , ,

字符引用和空白字符

看到最近有讨论,前些日子刚好也收集过一些资料,补充如下:

字符引用

在 html 中,有三种字符引用方式(参考 HTML5 规范):

  1. Named character references, 通过名称来引用。在 HTML 4.01 中称之为 Character entity references(字符实体引用)比如 &lt; &gt; &nbsp; &quot;, 这里有一份详细列表 named-character-references
  2. Decimal numeric character reference, 通过十进制数值来引用。比如 &#229; &#1048;
  3. Hexadecimal numeric character reference, 通过十六进制数值来引用。比如 &#xe5; &#x6C34;

在 HTML 4.01 中,上面 2 和 3 归为一种:Numeric character references(字符数值引用)。
这里有份非常好的对照表:XHTML Character Entity Reference
阅读全文 »

Tags: , , ,

Page 1 of 26123451020...Last »