Just a little convenient when you want to dynamically loading some scripts(and css).
(For example, within the tag of riot.js)
var rcLoader = function(files, then) {
var p = [];
function _load(type, url) {
return new Promise(function(resolve) {
var el;
var parent = 'body';
if (type == 'js') {
el = document.createElement('script');
el.async = true;
el.src = url;
}
else if (type == 'css') {
el = document.createElement('link');
el.type = 'text/css';
el.rel = 'stylesheet';
el.href = url;
parent = 'head';
}
el.onload = function() { resolve(url); };
document[parent].appendChild(el);
});
}
function _loader(file) {
if (file.match(/^js: (.+)/)) {
p.push(_load('js', RegExp.$1));
}
else if (file.match(/^css: (.+)/)) {
p.push(_load('css', RegExp.$1));
}
}
if (typeof files == 'string') { _loader(files); }
else {
files.forEach(function(v) { _loader(v); });
}
Promise.all(p).then(then);
};
rcLoader([
'js: //ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js',
'css: //cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css',
'js: //cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js',
], function() {
hljs.initHighlightingOnLoad();
$(function() {
$('#common').html('common');
});
});
The first argument may be a character string.
When you use IE or other old browser, use it.