javascript 判断JS文件是否加载完成

在平时的网站前端开发过程中,很多文件在本地测试成功,一旦上传服务器,由于受带宽、网速等因素的影响,很多特效实现不了,并且报错,那就是有可能JS文件没有加载完成的原因。

在“按需加载”的需求中,我们经常会判断当脚本加载完成时,返回一个回调函数,那如何去判断脚本的加载完成呢?

我们可以对加载的 JS 对象使用 onload 来判断(js.onload),此方法 Firefox2、Firefox3、Safari3.1+、Opera9.6+ 浏览器都能很好的支持,但 IE6、IE7 却不支持。曲线救国 —— IE6、IE7 我们可以使用 js.onreadystatechange 来跟踪每个状态变化的情况(一般为 loading 、loaded、interactive、complete),当返回状态为 loaded 或 complete 时,则表示加载完成,返回回调函数。

对于 readyState 状态需要一个补充说明:
在 interactive 状态下,用户可以参与互动。
Opera 其实也支持 js.onreadystatechange,但他的状态和 IE 的有很大差别。

具体实现代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
include_js<span style="color: #009900">(</span>file<span style="color: #009900">)</span> <span style="color: #009900">{</span>
 
      <span style="color: #003366;font-weight: bold">var</span> _doc <span style="color: #339933">=</span> document.<span style="color: #660066">getElementsByTagName</span><span style="color: #009900">(</span><span style="color: #3366CC">'head'</span><span style="color: #009900">)</span><span style="color: #009900">[</span><span style="color: #CC0000">0</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
 
      <span style="color: #003366;font-weight: bold">var</span> js <span style="color: #339933">=</span> document.<span style="color: #660066">createElement</span><span style="color: #009900">(</span><span style="color: #3366CC">'script'</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
      js.<span style="color: #660066">setAttribute</span><span style="color: #009900">(</span><span style="color: #3366CC">'type'</span><span style="color: #339933">,</span> <span style="color: #3366CC">'text/javascript'</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
      js.<span style="color: #660066">setAttribute</span><span style="color: #009900">(</span><span style="color: #3366CC">'src'</span><span style="color: #339933">,</span> file<span style="color: #009900">)</span><span style="color: #339933">;</span>
 
      _doc.<span style="color: #660066">appendChild</span><span style="color: #009900">(</span>js<span style="color: #009900">)</span><span style="color: #339933">;</span>
 
      <span style="color: #000066;font-weight: bold">if</span> <span style="color: #009900">(</span>document.<span style="color: #660066">all</span><span style="color: #009900">)</span> <span style="color: #009900">{</span> <span style="color: #006600;font-style: italic">//如果是IE</span>
 
            js.<span style="color: #660066">onreadystatechange</span> <span style="color: #339933">=</span> <span style="color: #003366;font-weight: bold">function</span> <span style="color: #009900">(</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
 
                    <span style="color: #000066;font-weight: bold">if</span> <span style="color: #009900">(</span>js.<span style="color: #660066">readyState</span> <span style="color: #339933">==</span> <span style="color: #3366CC">'loaded'</span> <span style="color: #339933">||</span> js.<span style="color: #660066">readyState</span> <span style="color: #339933">==</span> <span style="color: #3366CC">'complete'</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
 
                      <span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">'IE6、IE7 support js.onreadystatechange'</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
                  <span style="color: #009900">}</span>
 
           <span style="color: #009900">}</span>
 
      <span style="color: #009900">}</span>
 
      <span style="color: #000066;font-weight: bold">else</span> <span style="color: #009900">{</span>
 
             js.<span style="color: #000066">onload</span> <span style="color: #339933">=</span> <span style="color: #003366;font-weight: bold">function</span> <span style="color: #009900">(</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
 
                       <span style="color: #000066">alert</span><span style="color: #009900">(</span><span style="color: #3366CC">'Firefox、chrome and others support js.onload'</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
              <span style="color: #009900">}</span>
 
       <span style="color: #009900">}</span>
 
<span style="color: #009900">}</span>
 
include_js<span style="color: #009900">(</span><span style="color: #3366CC">'jquery.js'</span><span style="color: #009900">)</span><span style="color: #339933">;</span>