加载方式

形象图像化方法,见

http://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html

1、 script标签, 无 async defer, 则script文件下载和执行的过程, 会阻塞后面html标签的解析,进而影响页面渲染。

2、script标签, 有defer, 则script文件下载过程,不会阻塞后续html解析(即并发), 在所有html解析完毕后, 才执行下载的script文件。

3、script标签, 有async, 则script文件下载过程, 不会阻塞后续html解析(即并发),下载完毕后立刻执行, 执行过程会阻塞下载完时刻html解析点之后的html解析, script执行完毕后, hmtl解析立刻执行。

协程的解释:

http://ued.ctrip.com/blog/script-defer-and-async.html

使用 defer 和 async 属性, 可以加快页面渲染的速度。

但是如何才能保证js文件的前后依赖顺序, 下面介绍一个库。

js加载方法

同步加载库:

https://github.com/azev/JSLoad

JSLoad dynamicaly and synchronously loads CSS and JS files.

Usage:

  JSLoad.load(
[ '/plugins/style-sheet1.css'
, 'http://whatever-cdn.com/style-sheet2.css'
, 'https://cdnjs.cloudflare.com/ajax/libs/1140/2.0/1140.min.css'
, 'http://code.jquery.com/jquery-2.1.4.min.js'
, 'https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js'
]
, callback);

The callback function is called after all dependencies are loaded.
To avoid double loading, JSLoad will skip files with the same name.

异步加载库

https://github.com/alinoch/jsloader

JsLoader

A little and light script that loads asynchronously .js files and executes callback functions after certain files have been loaded or after all files are loaded. If an array of files is given they are loaded in the same order as their order in the array, so there shouldn't be any dependency issues.

Quick user guide

In order to use it just add jsLoader.min.js to your page. You can use the loadFiles method:

Synthax:

JsLoader.loadFiles({string|array} files, {function} callback, [{boolean} debug]);

Sample codes:

  • Load a single file:
JsLoader.loadFiles(
'localJsFile.js',
function() {
addMessage('1. Loaded a single file (localJsFile.js) and executed a callback');
}
);
  • Load more files and set the debug mode to on:
JsLoader.loadFiles(
['localJsFile.js', 'anotherLocalJsFile.js'],
function() {
addMessage('2. Loaded more files (localJsFile.js and anotherLocalJsFile.js) and executed a callback');
},
true
);
  • Load more files, do a callback for one file, then do a final callback:
JsLoader.loadFiles(
[
{src: 'localJsFile.js', callback: function() {console.log('callback: loaded localJsFile.js')}},
'anotherLocalJsFile.js'
],
function() {
addMessage('3. Loaded more files (localJsFile.js and anotherLocalJsFile.js), executed a callback after the 1st one was loaded (see console), then executed a callback after all files were loaded');
},
true
);
  • You can use an object as the parameter for the function:
JsLoader.loadFiles(
{
files: [
{src: 'localJsFile.js', callback: function() {console.log('callback: loaded localJsFile.js...')}},
{src: 'anotherLocalJsFile.js', callback: function() {console.log('callback: ... and loaded anotherLocalJsFile.js')}}
],
callback: function() {
addMessage('4. Loaded more files (localJsFile.js and anotherLocalJsFile.js), did a callback after each one, then did a final callback');
},
debug: true
}
);

Browser support & dependencies

This script does not require any library, since it is written in native javascript. It should work on all popular browsers (including the ancient IE6 :))

Demo

You can see some exapmples in action if you download and open the demo.html file.

jquery加载语句

https://api.jquery.com/jQuery.getScript/

jQuery.getScript( url [, success ] )Returns: jqXHR

Description: Load a JavaScript file from the server using a GET HTTP request, then execute it.

$.getScript( "ajax/test.js" )
.done(function( script, textStatus ) {
console.log( textStatus );
})
.fail(function( jqxhr, settings, exception ) {
$( "div.log" ).text( "Triggered ajaxError handler." );
});

javascript文件加载模式与加载方法的更多相关文章

  1. Javascript文件加载:LABjs和RequireJS

    传统上,加载Javascript文件都是使用<script>标签. 就像下面这样: <script type="text/javascript" src=&quo ...

  2. Activity有四种加载模式(转)

    Activity有四种加载模式: standard singleTop singleTask singleInstance 在多Activity开发中,有可能是自己应用之间的Activity跳转,或者 ...

  3. Activity的加载模式及Intent.setFlags

    在多Activity开发中,有可能是自己应用之间的Activity跳转,或者夹带其他应用的可复用Activity.可能会希望跳转到原来某个Activity实例,而不是产生大量重复的Activity. ...

  4. 区分Activity的四种加载模式

    在多Activity开发中,有可能是自己应用之间的Activity跳转,或者夹带其他应用的可复用Activity.可能会希望跳转到原来某个Activity实例,而不是产生大量重复的Activity. ...

  5. 区分Activity的四种加载模式【转载】

    此文为转载,文章来源:http://marshal.easymorse.com/archives/2950 文章作者:   Marshal's Blog 参考文章:http://blog.csdn.n ...

  6. JS脚本文件的位置对页面加载性能影响以及无阻塞脚本(javascript)模式

    JS的阻塞特性:当<script>出现的时候,页面必须等待脚本文件的加载.解析.执行完毕后才能继续进行页面的渲染.不管脚本文件是以内联形式还是外部引入的形式出现在<script> ...

  7. 高性能javascript 文件加载阻塞

    高性能javascript   javascript脚本执行过程中会中断页面加载,直到脚本执行完毕,此操作阻塞了页面加载,造成性能问题. 脚本位置和加载顺序:如果将脚本放在head内,那么再脚本执行完 ...

  8. JavaScript文件加载器LABjs API详解

    在<高性能JavaScript>一书中提到了LABjs这个用来加载JavaScript文件的类库,LABjs是Loading And Blocking JavaScript的缩写,顾名思义 ...

  9. 两种动态加载JavaScript文件的方法

    两种动态加载JavaScript文件的方法 第一种便是利用ajax方式,第二种是,动静创建一个script标签,配置其src属性,经过把script标签拔出到页面head来加载js,感乐趣的网友可以看 ...

随机推荐

  1. 「JLOI2015」城池攻占 解题报告

    「JLOI2015」城池攻占 注意到任意两个人的战斗力相对大小的不变的 可以离线的把所有人赛到初始点的堆里 然后做启发式合并就可以了 Code: #include <cstdio> #in ...

  2. 关于Autosar中的NM模块的理解

    本篇文章主要介绍AutoSar中关于NM模块的理解. 阅读本篇文章希望达到的目的: 1. NM(网络管理)是用来做什么的: 2. AutoSar中网络管理的原理: 3.项目实例介绍 1. NM(网络管 ...

  3. vue学习笔记(二)- 数据绑定、列表渲染、条件判断

    vue的数据绑定和列表渲染的造轮子 GitHub:八至 作者:狐狸家的鱼 双向数据绑定 Vue中数据的双向绑定-v-model 数据->页面 页面->数据 适用:input.select. ...

  4. 【CF1119E】Pavel and Triangles

    题目大意:有 N 种长度的边,第 i 种长度为 \(2^i\),给定一些数量的这些边,问最多可以组合出多少种三角形. 题解:应该是用贪心求解,不过选择什么样的贪心策略很关键. 首先分析可知,两个较大边 ...

  5. 分布式监控系统Zabbix--使用Grafana进行图形展示

      今天介绍一款高颜值监控绘图工具Grafana,在使用Zabbix监控环境中,通常我们会结合Grafana进行图形展示.Grafana默认没有zabbix作为数据源,需要手动给zabbix安装一个插 ...

  6. C# 下载文件 只利用文件的存放路径来下载

    第一种方式: 最简单的就是返回一个file类型的数据即FilePathResult类型的对象 string serverPath = ConfigurationManager.AppSettings[ ...

  7. 快速傅里叶变换(FFT)_转载

    FFTFFT·Fast  Fourier  TransformationFast  Fourier  Transformation快速傅立叶变换 P3803 [模板]多项式乘法(FFT) 参考上文 首 ...

  8. 发送HTTP_GET请求 表头application/json

    /** * 发送HTTP_GET请求 * 该方法会自动关闭连接,释放资源 * @param reqURL 请求地址(含参数) * @param decodeCharset 解码字符集,解析响应数据时用 ...

  9. 反汇编Dis解析

    目录 反汇编dis解析 COMM段BSS段 注释段 Bl指令 title: 反汇编Dis解析 tags: ARM date: 2018-10-21 18:02:58 --- 反汇编dis解析 关于段, ...

  10. flask 模版语言及信息传递

    if语句 格式: {% if command %} {% elif %} {% else %} {% endif %} 代码示例 flask_one.py #encoding:utf-8 from f ...