zoukankan      html  css  js  c++  java
  • 每日一库:ControlJS

    ControlJS是大牛steve souders的作品,官网:http://stevesouders.com/controljs/

    知识点:
    ①预加载js:

    var CJS =CJS || {};
    CJS.downloadScript = function(url) {
      CJS.dprint("downloading " + url);
      if ( CJS.bIE || CJS.bOpera ) { //ie或者 Opera
        CJS.downloadScriptImage(url);
      }
      else {
        CJS.downloadScriptObject(url);
      }
    };
    
    
    // Download a script as an image.
    // This puts it in the browser's cache, but doesn't execute it.
    CJS.downloadScriptImage = function(url) {
      var img = new Image();
      img.onload = function() { CJS.onloadCallback(url); };
      img.onerror = function() { CJS.onloadCallback(url); }; // Chrome does onerror (not onload).
      img.src = url;
    };
    
    
    // Download a script as an object.
    // This puts it in the browser's cache, but doesn't execute it.
    // Based on http://www.phpied.com/preload-cssjavascript-without-execution/
    CJS.downloadScriptObject = function(url) {
      if ( "undefined" === typeof(document.body) || ! document.body ) {
        // we need body for appending objects
        setTimeout("CJS.downloadScriptObject('" + url + "')", 50);
        return;
      }
    
      var obj = document.createElement('object');
      obj.data = url;
      obj.width  = 0;
      obj.height = 0;
      obj.onload = function() { CJS.onloadCallback(url); };
      obj.onerror = function() { CJS.onloadCallback(url); };
      //CJS.dprint("downloadScriptObject: appending " + url);
      document.body.appendChild(obj);
    };
    

      

    ②重写document.write
    有时候页面会有很多第三方插入广告,拖慢页面的渲染速度,这个时候要考虑重写一下document.write
    另参考:http://stylechen.com/rewrite-documentwrite.html


    ③为什么这个库貌似大家用的比较少?
    其一,是定义的时候较为繁琐,这个颇能吓走一部分人

    其二,有点致命的是用Image或者Object缓存js和执行js,要分别请求2次,这个对大访问量的网站来说可能是没办法接受的

  • 相关阅读:
    【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X
    【转载】Linux 文件系统的目录结构
    postgreSQL使用
    [转载] Linux启动过程详解-《别怕Linux编程》之八
    冒泡排序
    java值类型和引用类型
    冒泡排序法与二分查找法
    关系型数据库
    SQList的建表并添加数据练习
    数据存储——SQLite数据库存储
  • 原文地址:https://www.cnblogs.com/zhuzf/p/ControlJS.html
Copyright © 2011-2022 走看看