加载方式

形象图像化方法,见

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:

  1. JSLoad.load(
  2. [ '/plugins/style-sheet1.css'
  3. , 'http://whatever-cdn.com/style-sheet2.css'
  4. , 'https://cdnjs.cloudflare.com/ajax/libs/1140/2.0/1140.min.css'
  5. , 'http://code.jquery.com/jquery-2.1.4.min.js'
  6. , 'https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js'
  7. ]
  8. , 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:

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

Sample codes:

  • Load a single file:
  1. JsLoader.loadFiles(
  2. 'localJsFile.js',
  3. function() {
  4. addMessage('1. Loaded a single file (localJsFile.js) and executed a callback');
  5. }
  6. );
  • Load more files and set the debug mode to on:
  1. JsLoader.loadFiles(
  2. ['localJsFile.js', 'anotherLocalJsFile.js'],
  3. function() {
  4. addMessage('2. Loaded more files (localJsFile.js and anotherLocalJsFile.js) and executed a callback');
  5. },
  6. true
  7. );
  • Load more files, do a callback for one file, then do a final callback:
  1. JsLoader.loadFiles(
  2. [
  3. {src: 'localJsFile.js', callback: function() {console.log('callback: loaded localJsFile.js')}},
  4. 'anotherLocalJsFile.js'
  5. ],
  6. function() {
  7. 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');
  8. },
  9. true
  10. );
  • You can use an object as the parameter for the function:
  1. JsLoader.loadFiles(
  2. {
  3. files: [
  4. {src: 'localJsFile.js', callback: function() {console.log('callback: loaded localJsFile.js...')}},
  5. {src: 'anotherLocalJsFile.js', callback: function() {console.log('callback: ... and loaded anotherLocalJsFile.js')}}
  6. ],
  7. callback: function() {
  8. addMessage('4. Loaded more files (localJsFile.js and anotherLocalJsFile.js), did a callback after each one, then did a final callback');
  9. },
  10. debug: true
  11. }
  12. );

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. python 逻辑运算符问题

    1 正确 if('A' not in self.storageDevice.softVersion or\ 'B' not in self.storageDevice.softVersion or\ ...

  2. Palindrome Function HDU - 6156(数位dp)

    要求m-n内在l-r进制下的是回文数的总个数. dp[进制][从第j为开始][目前到达第k位] = 总的方案数 dfs枚举目前的到达的位置,这个数开始的位置,进制,前导零,限制条件,然后枚举的时候如果 ...

  3. POJ--1328 Radar Installation(贪心 排序)

    题目:Radar Installation 对于x轴上方的每个建筑 可以计算出x轴上一段区间可以包含这个点 所以就转化成 有多少个区间可以涵盖这所有的点 排序之后贪心一下就ok 用cin 好像一直t看 ...

  4. mysql视图和临时表的区别

    视图 视图是由从数据库的基本表中选出来的数据组成的逻辑窗口,它与基本表不同的是,视图是一个虚表.数据库中只存放视图的定义,而不存放视图包含的数据,这些数据仍存放在原来的基表中.所以基表中的数据如果发生 ...

  5. 记一次 HTTP信息头管理器使用 的重要性

    今天在测试中遇到了一个问题 使用JMeter时请求相关地址参数及方法都填写正确,但是相应数据返回始终不对,例如 查看取样器结果显示 200 正常,但响应数据不符合正常的结果. 经反复检查发现问题如下: ...

  6. 第二十七篇-新建Activity

    新建Activity实现页面之间的跳转与传值. layout1.xml <?xml version="1.0" encoding="utf-8"?> ...

  7. 第二十六篇-单击事件、Toast(提示框信息)

    单击事件有3种方法: 第一种: layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...

  8. PMP项目管理的49个过程,一张图让你全部了解

    项目管理的49个过程,看表格显得比较单调,印象也不是很深,所以今天小编就给大家发一张图片,可以用一张图就能生动又详细的了解PMP项目管理的49个过程.   大家看完是不是觉得一目了然了呢,图片上传后不 ...

  9. Mock4 moco框架中如何加入cookies

    新建一个 startupWithCookies.json,因为cookies也是请求当中带的,所以,要写在request里面,cookies是k-v的形式,就拿登陆来说吧,登陆以后会的cookies, ...

  10. POJ 1971 Parallelogram Counting (Hash)

          Parallelogram Counting Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6895   Acc ...