我們經常用動態創建 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 文件。
擴展閱讀: