讓JavaScript和其它資源並發下載的方法,javascript並發

來源:互聯網
上載者:User

讓JavaScript和其它資源並發下載的方法,javascript並發

在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");


javascript 怎將對象當數組,並以方法名作為下標來調用方法

第5行擷取自訂對象的屬性aproperty
第6行調用自訂對象的方法aMethod

第7行迴圈自訂對象的所有成員,包含屬性和方法
第8行擷取各個自訂對象成員的類型,分為屬性和方法兩種
 
怎將這兩個javascript合并

<script> 
//寫cookies函數 作者:翟振凱 
function SetCookie(name,value)//兩個參數,一個是cookie的名子,一個是值 
{ 
    var Days = 1; 
    var exp = new Date(); //new Date("December 31, 9998"); 
    exp.setTime(exp.getTime() + Days*24*60*60*1000); 
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
} 
function getCookie(name)//取cookies函數 
{ 
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); 
    if(arr != null) return unescape(arr[2]); return null; 

} 
function delCookie(name)//刪除cookie 
{ 
    var exp = new Date(); 
    exp.setTime(exp.getTime() - 1); 
    var cval=getCookie(name); 
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString(); 
} 

</script> 

<script> 
var value = getCookie("tianyi");
if(value==n......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.