yepnope 是一個體積極小的資源載入器,特色是能把「載入」與「執行」分離,並依條件決定要載哪一份腳本。以下依序說明語法、優缺點與各類實例。
yepnope({
test : Modernizr.geolocation,
yep : 'normal.js',
nope : ['polyfill.js', 'wrapper.js']
});
意義:若 Modernizr.geolocation 為真,載入 normal.js;為假則載入 polyfill.js 與 wrapper.js。
yepnope([
{
test: /* boolean(ish) 輸入條件 */,
yep: /* array (of strings) | string - 條件為真時載入的資源 */,
nope: /* array (of strings) | string - 條件為假時載入的資源 */,
both: /* array (of strings) | string - 條件真假皆載入 */,
load: /* array (of strings) | string - 條件真假皆載入 */,
callback: /* function (testResult, key) | object { key: fn } 回調函式 */,
complete: /* function 載入完成後執行的函式 */
},
...
]);
看程式碼更好懂:
yepnope([
{
load: 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
complete: function () {
if (!window.jQuery) {
yepnope('local/jquery.min.js'); // CDN 失敗就退回本機檔
}
}
},
{
load: 'jquery.plugin.js',
complete: function () {
jQuery(function () {
jQuery('div').plugin();
});
}
}
]);
其他載入器可能得寫成這樣:
someLoader('https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', function () {
if (!window.jQuery) {
someLoader('local/jquery.min.js', 'jquery.plugin.js');
// 注意這裡與 yepnope 的差異:yepnope 載入失敗後只需重新載入備用資源,
// 不會影響依賴該資源的其他檔案執行
} else {
someLoader('jquery.plugin.js');
}
});
直接傳入字串:
yepnope('/url/to/your/script.js');
傳入物件:
yepnope({ load: '/url/to/your/script.js' });
(callback 中 url 為載入的資源檔名、result 為 test 參數的結果、key 為使用 key mapping 時的縮寫名)
yepnope({
test: window.JSON,
load: '/url/to/your/script.js',
callback: function (url, result, key) {
// 執行到這裡時,該腳本剛執行完
alert('script.js has loaded!');
}
});
yepnope({
test: window.JSON,
nope: 'json2.js',
complete: function () {
var data = window.JSON.parse('{ "json": "string" }');
}
});
yepnope({
test: Modernizr.geolocation,
yep: { 'rstyles': 'regular-styles.css' },
nope: { 'mstyles': 'modified-styles.css',
'geopoly': 'geolocation-polyfill.js' },
callback: function (url, result, key) {
if (key === 'geopoly') {
alert('This is the geolocation polyfill!');
}
}
});
回調函式也可以寫成物件形式,為每個 key 分別指定:
yepnope({
test: Modernizr.geolocation,
yep: { 'rstyles': 'regular-styles.css' },
nope: { 'mstyles': 'modified-styles.css',
'geopoly': 'geolocation-polyfill.js' },
callback: {
'rstyles': function (url, result, key) {
alert('This is the regular styles!');
},
'mstyles': function (url, result, key) {
alert('This is the modified styles!');
},
'geopoly': function (url, result, key) {
alert('This is the geolocation polyfill!');
}
},
complete: function () {
alert('Everything has loaded in this test object!');
}
});
css! Prefix:可把任何副檔名的檔案當 CSS 載入。
yepnope('css!mystyles.php?version=1532');
preload! Prefix:預先載入資源到快取但不執行(下次 load 時才執行)。
yepnope({
load: 'preload!jquery.1.5.0.js',
callback: function (url, result, key) {
window.jQuery; // undefined
yepnope('jquery.1.5.0.js');
window.jQuery; // object
}
});
ie! Prefix(es):判斷是否為 IE 瀏覽器。除 ie! 外還支援 ie5、ie6、ie7、ie8、ie9、iegt5~iegt8、ielt7~ielt9 等寫法。
yepnope({
load: ['normal.js', 'ie6!ie7!ie-patch.js'] // 僅 ie6 或 ie7 載入 patch
});
今日視角補充:yepnope.js 已停止維護,其作用已被 ES Module 的原生 import() 動態匯入、以及 <link rel="preload">/<script type="module"> 等標準機制取代,新專案建議採用標準方案。