加载方式

形象图像化方法,见

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. photoshop学习3

    一.仿制图章工具 快捷键:S. 操作:先按住ALT键,再点击图片的一个地方,然后松开ALT和鼠标(这叫取样).之后到画布的另一个地方用鼠标绘画. 特点:绘画出和取样点一样的图像.这个工具原样复制了取样 ...

  2. MYSQL主从复制制作配置方案

    1. 主从复制机器配置 操作系统:centos7 x64 基于vagrant下的virtual box的虚拟机两台 master ip:192.168.21.11, slave ip 192.168. ...

  3. 使用Docker for Windows初体验

    https://www.baidu.com/link?url=61Kwadwh6h__2Vmjf7lAKVo1RjhsULAqERcMXYnYzkLKrRVpygwBJVnjultH8zbq& ...

  4. js 读取包含特殊字符的属性值

    在JS中对象的属性可以通过两种方式访问:object.property和object["property"]. 包含特殊字符的属性只能以此方式访问: object["pr ...

  5. 【洛谷P2568】GCD

    题目大意:给定整数 \(N\),求\(1\le x,y\le N\) 且 \(gcd(x,y)\) 为素数的数对 \((x,y)\) 有多少对. 题解: \[ \sum_{p \in \text { ...

  6. gevent多协程运用

    #导包 import gevent #猴子补丁 from gevent import monkey monkey.patch_all() from d8_db import ConnectMysql ...

  7. 在vue中使用import()来代替require.ensure()实现代码打包分离

    最近看到一种router的写法 import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) const login = ...

  8. 初识JSP知识

    一.jsp概述 JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP实际上就是Servlet. jsp = html ...

  9. Tomcat 一般异常处理方式

    1.启动时出现: Exception starting filter struts2 java.lang.NoClassDefFoundError: org/apache/commons/lang3/ ...

  10. mysql 游标的使用方法

    BEGIN /*计算用户提成总金额*/ declare amountPrice,pays,rates,goodsPrice DECIMAL(10,2) DEFAULT 0; DECLARE flag ...