国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

讓JavaScript和其它資源并發下載的方法

2019-11-20 14:02:23
字體:
來源:轉載
供稿:網友

在IE6/7里JavaScript會從兩個方面阻礙頁面呈現:
script標簽下面的網頁資源在script加載完之前會停止請求、下載。
script標簽下面的html元素在script加載完之前會停止渲染。

在ie6/7 firefox2/3 Safari3 Chrome1 和 opera下 script標簽會阻礙下載:

雖然在ie8,safari4,chrome2下script可以并發,但依然阻礙了其他資源的下載:

有6種方法可以使script與其他資源并行下載:

1.XHR eval ― 通過XHR(XMLHttpRequest 對象)下載script,然后用eval方法執行XHR的responseText
2.XHR Injection ― 通過XHR下載script,然后建立一個script標簽并把它插入文檔中(body或者head標簽內),接著把script標簽的text屬性設置為XHR的responseText的值
3.XHR in Iframe ― 把script標簽放到一個iframe里,通過iframe下載它
4.Script DOM Element ― 創建script標簽并把它的src屬性指向你的腳本地址
5.Script Defer ― 添加script標簽的defer屬性,這個只在ie中有效,但firefox3.1也支持這個屬性了
6.使用document.write方法在頁面中寫入<script src="">,這個只在ie里有效

可以通過Cuzillion查 看各個方法的使用例子。

如果有一些內聯腳本需要在外部腳本執行后才能執行,那就需要同步(synchronize)他們了。稱作”coupling”,Coupling Asynchronous Scripts 這篇文章介紹了一些目前可以實現“coupling”的方法。

headjs,能使JS并發下載(但是順序執行):http://headjs.com/

復制代碼 代碼如下:

head.js("/path/to/jquery.js", "/google/analytics.js", "/js/site.js", function() { 
  // all done 
}); 
  
// the most simple case. load and execute single script without blocking. 
head.js("/path/to/file.js"); 
  
// load a script and execute a function after it has been loaded 
head.js("/path/to/file.js", function() { 
  
}); 
  
// load files in parallel but execute them in sequence 
head.js("file1.js", "file2.js", ... "fileN.js"); 
  
// execute function after all scripts have been loaded 
head.js("file1.js", "file2.js", function() { 
  
}); 
  
// files are loaded in parallel and executed in order they arrive 
head.js("file1.js"); 
head.js("file2.js"); 
head.js("file3.js"); 
  
// the previous can also be written as 
head.js("file1.js").js("file1.js").js("file3.js");

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄陵县| 城固县| 大厂| 景洪市| 株洲县| 三门峡市| 西城区| 铜山县| 宁波市| 洛阳市| 息烽县| 长岛县| 葫芦岛市| 荣昌县| 广元市| 宁河县| 田东县| 青阳县| 洪雅县| 弥勒县| 海原县| 阿尔山市| 临湘市| 乐亭县| 鄂温| 广安市| 嘉鱼县| 沅江市| 瓦房店市| 河北省| 琼结县| 桂林市| 新郑市| 拜泉县| 阿克苏市| 潼南县| 桃园市| 瑞丽市| 万年县| 阳高县| 都匀市|