Skip to content

Instantly share code, notes, and snippets.

@bh-lay
Created December 25, 2016 15:41
Show Gist options
  • Select an option

  • Save bh-lay/207ad75f2fd5e4a131b458a1fa9374dd to your computer and use it in GitHub Desktop.

Select an option

Save bh-lay/207ad75f2fd5e4a131b458a1fa9374dd to your computer and use it in GitHub Desktop.
简短的加载JS方法
/**
* 加载JS
*/
function LoadScript(url, callback){
var urls = typeof url === 'string' ? [url] : url;
if (!Array.isArray(urls)){
throw 'argument type error';
return;
}
if (urls.length <= 0){
callback();
return;
}
var head = document.head;
var script = document.createElement('script');
script.src = urls[0];
script.async = false;
script.type = 'text/javascript'
script.charset = 'utf-8'
head.appendChild(script);
script.onload = script.onerror = function(){
script.onload = script.onerror = null;
LoadScript(urls.slice(1), callback);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment