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,感乐趣的网友可以看 ...
随机推荐
- kafka问题集锦
一. org.apache.kafka.common.errors.TimeoutException: Batch Expired 问题描述:通过java客户端访问kafka服务器,当生产者线程向ka ...
- Wannafly挑战赛29-A御坂美琴 (dfs+map)
链接:https://ac.nowcoder.com/acm/contest/271/A来源:牛客网 御坂美琴 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言 ...
- Spring -- 自定义转换器
Spring 定义了 3 种类型的转换器接口,实现任意一个转换器接口都可以作为自定义转换器注册到 ConversionServiceFactoryBean 中: Converter<S,T> ...
- ava EE 7 - Injection into Runnable/Callable object ejb entityManager Concurrency ManagedExecutorService 异步调用如何获取context
或者直接把 MyTask类内嵌如MyBean中,这样可以在myBean中inject 数据库连接,在内嵌类内访问. java ee 引入了并发执行.因为是在服务器执行并发,所以要用java ee包里面 ...
- java 不定长参数
一,不定长参数的规定 一个方法只能有一个不定长参数,并且这个不定长参数必须是该方法的最后一个参数. 示例: public class VariArgs { public static void mai ...
- http请求415错误Unsupported Media Type
王子乔 每一个认真生活的人,都值得被认真对待 http请求415错误Unsupported Media Type 之前用了封装的ajax,因为请求出了点问题,我试了下jQuery的$.ajax,报出了 ...
- keepalived+LVS实现网站负载均衡和HA
如上图所示,102和103是内网nginx服务器,100和101是边界LB,clinet是1,这个实验是为了实现在LB上虚拟出一个VIP,client通过访问该VIP,来动态负载到两台内网nginx服 ...
- php+mysql简单的添加和删除小案例
1.分析 index.php是呈现列表,通过点击列表页上的添加和删除按钮,对列表页上面的进行操作 index.php TODO:要将数据库里面的内容呈现到页面中 (1)连接数据库 (2)查询数据 (3 ...
- PEP8规范
目录 一 代码编排 二 文档编排 三 空格使用 四 注释 五 文档描述 六 命名规范 七 编码建议 代码编排 1缩进,4个空格,不用tab键(因为可能不同系统tab的空格数不一定) 2每行最大长度79 ...
- thinkphp 攻略
php框架 一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难, ...