DIV CSS 佈局教程網

利用 script 的 type 屬性實現 JavaScript 預加載但不執行
編輯:關於JavaScript     

我們經常用動態創建 JavaScript 的方式,來實現文件的無阻塞(Non-blocking)與並行下載(Parallel)。其實還有一種思路:為 script 添加自定義的 type 屬性(例如 text/cache),同樣能達到「預加載但不執行」的效果:

var doc = document,
el = doc.createElement("script"),
head = doc.getElementsByTagName('head')[0];
el.src = "https://yui.yahooapis.com/2.8.2r1/build/yahoo-dom-event/yahoo-dom-event.js";
el.type = "text/cache";
head.insertBefore(el, head.firstChild);

註:Firefox 3.6.x 不會請求該 JavaScript 文件。

擴展閱讀:

  • 《Preload CSS/JavaScript without execution》
  • 《Preloading JS and CSS as Print Stylesheets》
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved