五月 7th, 2010

不使用execution预加载

前端开发, by army.

预加载有很多种,dom预加载、iframe预加载、ajax预加载等等。但是有另外一种简洁安全的做法:

  • 对ie使用Image来预加载内容
  • 其它浏览器使用动态的object标签

例如:

window.onload = function () {

    var i = 0,
        max = 0,
        o = null,

        // list of stuff to preload
        preload = [
            'http://tools.w3clubs.com/pagr2/.sleep.expires.png',
            'http://tools.w3clubs.com/pagr2/.sleep.expires.js',
            'http://tools.w3clubs.com/pagr2/.sleep.expires.css'
        ],
        isIE = navigator.appName.indexOf('Microsoft') === 0;

    for (i = 0, max = preload.length; i < max; i += 1) {

        if (isIE) {
            new Image().src = preload[i];
            continue;
        }
        o = document.createElement('object');
        o.data = preload[i];

        // IE stuff, otherwise 0x0 is OK
        //o.width = 1;
        //o.height = 1;
        //o.style.visibility = "hidden";
        //o.type = "text/plain"; // IE
        o.width  = 0;
        o.height = 0;

        // only FF appends to the head
        // all others require body
        document.body.appendChild(o);
    }

};

Back Top

回复自“不使用execution预加载”

  1. 没有任何评论。
  1. 没有任何引用。

发表回复

Back Top