js文件引用方式及其同步执行与异步执行
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp74
任何以appendChild(scriptNode) 的方式引入的js文件都是异步执行的 (scriptNode 需要插入document中,只创建节点和设置 src 是不会加载 js 文件的,这跟 img 的与加载不同 )
html文件中的<script>标签中的代码或src引用的js文件中的代码是同步加载和执行的
html文件中的<script>标签中的代码使用document.write()方式引入的js文件是异步执行的
html文件中的<script>标签src属性所引用的js文件的代码内再使用document.write()方式引入的js文件是同步执行的
1、
<script>
//同步加载执行的代码
</script>
2、
<script src="xx.js"></script> //同步加载执行xx.js中的代码
3、
<script>
document.write('<script src="xx.js"><\/script>'); //异步加载执行xx.js中的代码
</script>
4、
<script src="xx.js"></script>
xx.js中有下面代码:
document.write('<script src="11.js"><\/script>');
document.write('<script src="22.js"><\/script>');
则xx.js和11.js、22.js 都是同步加载和执行的。
如果 xx.js 以插入方式异步加载,则 11.js 和 22.js 仍然是同步加载的(异步中的同步,即,这2个文件的加载是分先后次序的)
测试:在11中 alert, 22中 document.write() ,可以看到 22中写入语句被阻塞
5、
下面这种方式,xx.js会在appendChild执行之后异步加载执行
var script = document.createElement("script");
script.setAttribute("src","xx.js");
documenrt.getElementsByTagName("head")[0].appendChild(script);
一个加载 js 文件的 函数:
var loadJS = function(url,callback){ var head = document.getElementsByTagName('head')[0], script = document.createElement('script'); script.src = url; script.type = "text/javascript"; head.appendChild( script);
script.onload = script.onreadystatechange = function(){
//script 标签,IE 下有 onreadystatechange 事件, w3c 标准有 onload 事件
//这些 readyState 是针对IE8及以下的,W3C 标准因为script 标签没有这个 onreadystatechange 所以也不会有 this.readyState ,
// 好在文件加载不成功 onload 不会执行,(!this.readyState) 是针对 W3C标准的
if ((!this.readyState) || this.readyState == "complete" || this.readyState == "loaded" ){
callback(); }else{ alert("can not load the js file") } }}
对于第4点的测试(其中插入 alert 很容易看到加载时阻塞的)
tryjs.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="tryjs.js"
onload="if(!document.all){console.log('outer js callback, not IE');}"
onreadystatechange="console.log('outer js callback ',this.readyState,' IE');"></script>
<body>
</body>
</html>
tryjs.js
console.log('write begin');
document.write('<script src="try.1.js" onreadystatechange="console.log(\'file 1 callback \',this.readyState,\' IE\');" onload="if(!document.all){console.log(\'file 1 callback,NOT IE \');}"><\/script>');
document.write('<script src="try.2.js" onreadystatechange="console.log(\'file 2 callback \',this.readyState,\' IE\');" onload="if(!document.all){console.log(\'file 2 callback,NOT IE \');}"><\/script>');
console.log('write finished');
try.1.js
console.log('loadjs 1 begin');
console.log('loadjs 1 finished');
try.2.js
console.log('loadjs 2 begin');
console.log('loadjs 2 finished');
测试结果(file 2 和 file 1 的 callback complete 在IE7\8\9次序不确定)
IE 7:
日志: outer js callback loading IE
日志: outer js callback loaded IE
日志: write begin
日志: write finished
日志: outer js callback complete IE
日志: file 1 callback loading IE
日志: file 2 callback loading IE
日志: loadjs 1 begin
日志: loadjs 1 finished
日志: loadjs 2 begin
日志: loadjs 2 finished
日志: file 2 callback complete IE
日志: file 1 callback complete IE
IE8:
日志: outer js callback loading IE
日志: outer js callback loaded IE
日志: write begin
日志: write finished
日志: outer js callback complete IE
日志: file 1 callback loading IE
日志: file 2 callback loading IE
日志: loadjs 1 begin
日志: loadjs 1 finished
日志: loadjs 2 begin
日志: loadjs 2 finished
日志: file 2 callback complete IE
日志: file 1 callback complete IE
IE9:
日志: write begin
日志: write finished
日志: outer js callback complete IE
日志: file 1 callback loading IE
日志: file 2 callback loading IE
日志: loadjs 1 begin
日志: loadjs 1 finished
日志: loadjs 2 begin
日志: loadjs 2 finished
日志: file 1 callback complete IE
日志: file 2 callback complete IE
FIREFOX:
write begin
write finished
outer js callback, not IE
loadjs 1 begin
loadjs 1 finished
file 1 callback,NOT IE
loadjs 2 begin
loadjs 2 finished
file 2 callback,NOT IE
CHROME:
write begin
write finished
outer js callback, not IE
loadjs 1 begin
loadjs 1 finished
file 1 callback,NOT IE
loadjs 2 begin
loadjs 2 finished
file 2 callback,NOT IE
js文件引用方式及其同步执行与异步执行的更多相关文章
- 解决HTML加载时,外部js文件引用较多,影响页面打开速度问题
解决HTML加载时,外部js文件引用较多,影响页面打开速度问题 通常HTML文件在浏览器中加载时,浏览器都会按照<script>元素在页面中出现的先后顺序,对它们依次加载,一旦加载的j ...
- 测开之路九十七:js的引用方式
第一种:引用外部js文件 准备一个js文件 <!-- 引用外部的js --><script src="../js/js01.js"></script& ...
- 深入理解 JS 引擎执行机制(同步执行、异步执行以及同步中的异步执行)
首先明确两点: 1.JS 执行机制是单线程. 2.JS的Event loop是JS的执行机制,深入了解Event loop,就等于深入了解JS引擎的执行. 单线程执行带来什么问题? 在JS执行中都是单 ...
- 解决加载WEB页面时,由于JS文件引用过多影响页面打开速度的问题
1.一般做法 一般我们会把所有的<script>元素都应该放在页面的<head>标签里,但由于是顺序加载,因此只有当所有JavaScript代码都被依次下载.解析和执行完之后, ...
- arcgis api for js自定义引用方式
(1)常规模式 即arcgis js常见的模块引用方式,采用 require-function 模式,function的参数与require一一对应即可(dojo/domReady!比较特殊,无需 ...
- jsp文件引入js文件的方式(项目部署于web容器中)
在页面中引入javascript文件的方式是多种多样的,本文介绍两种. 通过<script>标签插入js文件 通过这种方式引入的js,写对js文件和jsp文件的路径很重要.下面给出一个项目 ...
- js文件引用的问题顺带复习css引用
js文件包含在<script>块中用scr引用,css在link和@import来引用,css不是本篇的重点,直接引用一个博主的总结: “ 区别1:link是XHTML标签,除了加载CSS ...
- js文件引用js文件
我的问题是: a.jsp b.js c.js a.jsp 需要引用 b.js 里面的内容 <head> <script type="text/javascript& ...
- js封装,一个JS文件引用多个JS文件
(function() { //加载 varobj = {}; /** * 动态加载脚本函数 * @param url 要加载的脚本路径 * @param callback ...
随机推荐
- year:2017 month:7 day:18
2017-07-18 JavaScript 1javascript的控制语句 (1) if语句 (2)if(){}else 语句 (3) if(){} else if(){ }语句 (4)switch ...
- POJ3660 Cow Contest floyd传递闭包
Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...
- PDF修改器
亲测可用的绿色版PDF修改器供大家分享使用 下载地址:http://pan.baidu.com/s/1pLPnhQb
- 双向循环链表(C语言描述)(三)
代码清单 // linkedlist.h #ifndef __LINKEDLIST_H__ #define __LINKEDLIST_H__ #include <assert.h> #in ...
- swift UILabel多行显示时 计算UILable的高度(可用于UILable高度自适应)
代码如下 func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{ let label:UILabel = ...
- mysql创建定时任务,每月1号删除上月数据
1.创建存储过程: CREATE DEFINER=`gzy`@`%` PROCEDURE `delLastMonth`() BEGIN DECLARE lastmonth int; SET lastm ...
- ubuntu中安装搜狗输入法
1.查看系统中是否安装fcitx,libss2-1的依赖包,查看命令 dpkg -I | grep fcitx dpkg -I | grep libssh 没有安装的可以如下图命令安装 2.接下来我 ...
- XtraBackup应用说明(支持TokuDB)
背景: 关于物理备份工具xtrabackup的一些说明可以先看之前写过的文章说明:XtraBackup 安装使用和xtrabackup 使用说明(续),本篇文章将介绍xtrabackup在使用中的注意 ...
- SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
一.建立一个简单的jsp页面. 我们在建好的jsp的页面中加入一个超链接:<a href="${pageContext.request.contextPath}/download&qu ...
- linux 下 Fatal error: Class ‘mysqli’ not found in
先试用这种方法 http://blog.csdn.net/u010429424/article/details/43063211 我不知道自己安装的php 没他们路径,所以用了以下这种方法处理,并且不 ...