JavaScript延迟加载

延迟加载javascript,也就是页面加载完成之后再加载javascript,也叫on demand(按需)加载,一般有一下几个方法:

1. DOM

head append script tag

window.onload = function() {

setTimeout(function(){

// reference to

var head = document.getElementsByTagName('head')[0];

// a new CSS

var css = document.createElement('link');

css.type = "text/css";

css.rel = "stylesheet";

css.href = "new.css";

// a new JS

var js = document.createElement("script");

js.type = "text/javascript";

js.src = "new.js";

// preload JS and CSS

head.appendChild(css);

head.appendChild(js);

// preload image

new Image().src = "new.png";

}, 1000);

};

2. document.write

function include(script_filename) {

document.write('<' + 'script');

document.write(' language="javascript"');

document.write(' type="text/javascript"');

document.write(' src="' + script_filename + '">');

document.write('');

}

var which_script = '1.js';

include(which_script);

3. Iframe

和第一种类似,但是script tag是放到iframe的document里面。

window.onload = function() {

setTimeout(function(){

// create new iframe

var iframe = document.createElement('iframe');

iframe.setAttribute("width", "0");

iframe.setAttribute("height", "0");

iframe.setAttribute("frameborder", "0");

iframe.setAttribute("name", "preload");

iframe.id = "preload";

iframe.src = "about:blank";

document.body.appendChild(iframe);

// gymnastics to get reference to the iframe document

iframe = document.all ? document.all.preload.contentWindow : window.frames.preload;

var doc = iframe.document;

doc.open(); doc.writeln(""); doc.close();

// create CSS

var css = doc.createElement('link');

css.type = "text/css";

css.rel = "stylesheet";

css.href = "new.css";

// create JS

var js = doc.createElement("script");

js.type = "text/javascript";

js.src = "new.js";

// preload CSS and JS

doc.body.appendChild(css);

doc.body.appendChild(js);

// preload IMG

new Image().src = "new.png";

}, 1000);

};

4. Iframe static page

直接把需要加载东西放到另一个页面中

window.onload = function() {

setTimeout(function(){

// create a new frame and point to the URL of the static

// page that has all components to preload

var iframe = document.createElement('iframe');

iframe.setAttribute("width", "0");

iframe.setAttribute("height", "0");

iframe.setAttribute("frameborder", "0");

iframe.src = "preloader.html";

document.body.appendChild(iframe);

}, 1000);

};

5. Ajax eval

用ajax下载代码,然后用eval执行

6. Ajax Injection

用ajax下载代码,建立一个空的script tag,设置text属性为下载的代码

7. async 属性(缺点是不能控制加载的顺序)

感谢原著作者,谢谢

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 有些 js 代码并不是页面初始化的时候就立刻需要的,而稍后的某些情况才需要的。延迟加载就是一开始并不加载这些暂时不...
    落花的季节阅读 5,701评论 0 3
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 12,378评论 2 17
  • 以下是常用的代码收集,学习用。转自豪情博客园 1. PC - js 返回指定范围的随机数(m-n之间)的公式 re...
    自由加咖啡阅读 4,615评论 0 1
  • 首先我们先来看一下Script标签的各项属性: script标签也支持HTML中的全局属性: 下面我们来看看一看j...
    tobAlier阅读 4,798评论 0 2
  • 我不是一个热爱运动的人,从我170的身高,58的体重就可以看出来. 原因很简单,因为运动太累了。我就是不懂为什么那...
    小本阅读 11,010评论 29 103