54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
var Include = function(cfg) {
|
|
this.cfg = cfg;
|
|
|
|
this._init();
|
|
};
|
|
|
|
Include.prototype = {
|
|
constructor: Include,
|
|
|
|
_init: function() {
|
|
var c = this.cfg;
|
|
|
|
if (c.async !== false)
|
|
c.async = true;
|
|
|
|
this.$container = $('#' + c.id);
|
|
},
|
|
|
|
fetch: function() {
|
|
var c = this.cfg,
|
|
self = this;
|
|
|
|
return $.ajax({
|
|
url: c.src,
|
|
type: 'GET',
|
|
dataType: 'html',
|
|
async: c.async,
|
|
success: function(html) {
|
|
self.$container.html(html);
|
|
c.onload && c.onload(html);
|
|
}
|
|
});
|
|
}
|
|
};
|
|
// 需要引入的代码片段
|
|
var includes = [{
|
|
id: 'header',
|
|
src: '/common/footer.html',
|
|
onload: function() {
|
|
// console.log('...header loaded...');
|
|
}
|
|
}, {
|
|
id: 'footer',
|
|
src: '/common/footer.html',
|
|
onload: function() {
|
|
// console.log('...footer loaded...');
|
|
}
|
|
}];
|
|
|
|
$.each(includes, function(i, cfg) {
|
|
if ($('#' + cfg.id).length) {
|
|
new Include(cfg).fetch();
|
|
}
|
|
}); |