当前位置: 首页 > 工具软件 > ControlJS > 使用案例 >

每日一库:ControlJS

潘弘博
2023-12-01

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次,这个对大访问量的网站来说可能是没办法接受的

转载于:https://www.cnblogs.com/zhuzf/archive/2013/01/21/ControlJS.html

 类似资料: