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,感乐趣的网友可以看 ...
随机推荐
- PyCharm创建自定义代码段(JetBrains系列通用)
创建自定义代码段或者修改代码段:(用习惯了VSCode的main格式,固而改下)
- eureka集群基于DNS配置方式
https://www.cnblogs.com/relinson/p/eureka_ha_use_dns.html 最近在研究spring cloud eureka集群配置的时候碰到问题:多台eu ...
- 【CF1141G】Privatization of Roads in Treeland
题目大意:给定一个 N 个点的无根树,现给这个树进行染色.定义一个节点是坏点,若满足与该节点相连的至少两条边是相同的颜色,求至多有 k 个坏点的情况下最少需要几种颜色才能进行合法染色. 题解:考虑一个 ...
- 如何测试连接MsSQL数据库-------UDL文件
http://www.xinnet.com/service/cjwt/idc/sjk/1360.html 如果您所使用的 SQL Server 数据库连不上,可以通过这个方法进行测试数据库连接. 温馨 ...
- 文献笔记:Genome-wide associations for birth weight and correlations with adult disease
该文献纳入了EGG(Early Growth Genetics Consortium)和UK biobank两大数据库,分为欧洲祖先和非欧洲祖先群体.这两个数据用到的样本量分别如下: Early Gr ...
- 在spring中如何生成一个bean (一个对象,比如jedis的连接池对象)【我】
在spring中,要想生成一个单例对象(比如jedis的连接池对象) 方法1: 在 spring中用 bean 标签生成(反正就是让spring生成并管理单例的对象) 方法2: 把要生成的单例对象类, ...
- C 线程学习记录
"互斥锁"(Mutual exclusion,缩写 Mutex),防止多个线程同时读写某一块内存区域. 这时的解决方法,就是在门口挂n把钥匙.进去的人就取一把钥匙,出来时再把钥匙挂 ...
- javaSE eclipse tomocat安装与配置
---恢复内容开始--- javaSE 下载: 第一步:百度收索jdk downlaod 下载地址:https://www.oracle.com/technetwork/ja ...
- C++学习之回调函数
回调函数: 通过函数指针进行调用的函数. 回调函数不是由实现方进行调用,而是将函数指针传入,在特殊条件或者状态下进行触发调用. 譬如: 在自定义按钮控件时,当点击按钮,触发点击事件,调用指定函数. 注 ...
- 基于TC做流量控制
1 模拟延迟传输简介 netem 与 tc: netem 是 Linux 2.6 及以上内核版本提供的一个网络模拟功能模块.该功能模块可以用来在性能良好的局域网中,模拟出复杂的互联网传输性能,诸如低带 ...