javascript文件加载模式与加载方法
加载方式
形象图像化方法,见
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.
version added: 1.0jQuery.getScript( url [, success ] )
urlType: StringA string containing the URL to which the request is sent. successA callback function that is executed if the request succeeds.$.getScript( "ajax/test.js" )
.done(function( script, textStatus ) {
console.log( textStatus );
})
.fail(function( jqxhr, settings, exception ) {
$( "div.log" ).text( "Triggered ajaxError handler." );
});
javascript文件加载模式与加载方法的更多相关文章
- Javascript文件加载:LABjs和RequireJS
传统上,加载Javascript文件都是使用<script>标签. 就像下面这样: <script type="text/javascript" src=&quo ...
- Activity有四种加载模式(转)
Activity有四种加载模式: standard singleTop singleTask singleInstance 在多Activity开发中,有可能是自己应用之间的Activity跳转,或者 ...
- Activity的加载模式及Intent.setFlags
在多Activity开发中,有可能是自己应用之间的Activity跳转,或者夹带其他应用的可复用Activity.可能会希望跳转到原来某个Activity实例,而不是产生大量重复的Activity. ...
- 区分Activity的四种加载模式
在多Activity开发中,有可能是自己应用之间的Activity跳转,或者夹带其他应用的可复用Activity.可能会希望跳转到原来某个Activity实例,而不是产生大量重复的Activity. ...
- 区分Activity的四种加载模式【转载】
此文为转载,文章来源:http://marshal.easymorse.com/archives/2950 文章作者: Marshal's Blog 参考文章:http://blog.csdn.n ...
- JS脚本文件的位置对页面加载性能影响以及无阻塞脚本(javascript)模式
JS的阻塞特性:当<script>出现的时候,页面必须等待脚本文件的加载.解析.执行完毕后才能继续进行页面的渲染.不管脚本文件是以内联形式还是外部引入的形式出现在<script> ...
- 高性能javascript 文件加载阻塞
高性能javascript javascript脚本执行过程中会中断页面加载,直到脚本执行完毕,此操作阻塞了页面加载,造成性能问题. 脚本位置和加载顺序:如果将脚本放在head内,那么再脚本执行完 ...
- JavaScript文件加载器LABjs API详解
在<高性能JavaScript>一书中提到了LABjs这个用来加载JavaScript文件的类库,LABjs是Loading And Blocking JavaScript的缩写,顾名思义 ...
- 两种动态加载JavaScript文件的方法
两种动态加载JavaScript文件的方法 第一种便是利用ajax方式,第二种是,动静创建一个script标签,配置其src属性,经过把script标签拔出到页面head来加载js,感乐趣的网友可以看 ...
随机推荐
- su命令详解
-----------------------------------------------------------------su 权限设置[root@localhost ~]# vim /etc ...
- webpack入门(四)webpack的api 2 module
接着介绍webpack的module. module Options affecting the normal modules (NormalModuleFactory) 这些选项影响普通的模块 m ...
- Codeforces Round #493 (Div. 2)
C - Convert to Ones 给你一个01串 x是反转任意子串的代价 y是将子串全部取相反的代价 问全部变成1的最小代价 两种可能 一种把1全部放到一边 然后把剩下的0变成1 要么把所有的 ...
- [USACO18JAN]Sprinklers
[USACO18JAN]Sprinklers 一个矩形要符合什么条件 右上角的右上有点,左下角的左下有点 所以每列的选择高度为一个区间,小于后缀最大值大于前缀最小值(不管是作为右上角还是左下角) 然后 ...
- [HEOI2014]平衡
[HEOI2014]平衡 转化为求选择k个数,和为(n+1)*k的方案数 保证,每个数[1,2*n+1]且最多选择一次. 限制k个很小,所以用整数划分的第二种方法 f[i][j],用了i个,和为j 整 ...
- tyvj/joyoi 1305 最大子序和
带了一个转化的单调队列裸题. 转化为前缀和相减即可. 有一点需要注意:从0开始入队而不是1,因为要统计第一个. (从网上找的对拍程序,结果别人写错了) /** freopen("in.in& ...
- 洛谷P1399 快餐店
题意:在基环树上找一点,使得这个点到所有点的距离最大值最小.这个点可以在某条边上. 解:很容易想到找出直径然后/2对吧...这里的直径是指任意两点间最短距离的最大值. 然而我这个SB冥思苦想了半天之后 ...
- 题解-洛谷P1020P导弹拦截(求单调序列长度的优化)
https://www.luogu.org/problemnew/show/P1020 (原题链接) 第一问就是求最长不上升子序列的长度,自然就想到了c++一本通里动态规划里O(n^2)的算法,但题目 ...
- 团体程序设计天梯赛(CCCC) L3012 水果忍者 上凸或下凹的证明
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code #include <cstdio> #include ...
- 安装SVN并使用IDEA检出项目
首先去下载小王八:https://tortoisesvn.net/downloads.html 下载完毕,打开 .. ..注意勾选command line工具 .. .. 下一步,打开IDEA,配置S ...