Last active
October 28, 2016 20:24
-
-
Save zenithtekla/2305dc4790f69f33ba68fa030c5d2746 to your computer and use it in GitHub Desktop.
Improvement on the loadScript, still not working with AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// src: https://github.com/zenithtekla/mantisbt/blob/r6/plugins/UTILS_plugin/bower_components/mantis_extended_kernel/client/js/buildscript.js | |
// further ref: http://stackoverflow.com/a/950146/5828821 | |
var loadScript = function(arr){ | |
var loaded = []; | |
var ignored = []; | |
for (var i = 0; i < arr.length; i++) { | |
var _ = arr[i]; | |
if (typeof _.ref === 'string') { _.ref = [_.ref]; } | |
var type = _.ref[0].slice(_.ref[0].lastIndexOf('.')+1); | |
switch (type) { | |
case 'js': | |
_.ref.forEach(function(file){ | |
var scripts = document.getElementsByTagName("script"); | |
for(var i = 0; i < scripts.length; i++){ | |
var src = scripts[i].getAttribute('src'); | |
if(src) | |
if(src.indexOf(file) > -1){ | |
ignored.push(file); | |
return; | |
} | |
} | |
var link = document.createElement('script'); | |
link.src = (_.path) ? _.path + file : file; | |
link.type = 'text/javascript'; link.async = _.async || false; | |
document.getElementsByTagName('body')[0].appendChild(link); | |
console.log(link); | |
loaded.push(file); | |
}); | |
break; | |
case 'css': | |
_.ref.forEach(function(file){ | |
for(var i = 0; i < document.styleSheets.length; i++){ | |
if(document.styleSheets[i].href.indexOf(file) > -1){ | |
ignored.push(file); | |
return; | |
} | |
} | |
var head = document.getElementsByTagName('head')[0]; | |
var link = document.createElement('link'); | |
link.rel = 'stylesheet'; | |
link.type = _.type || 'text/css'; | |
link.href = (_.path) ? _.path + file : file; | |
head.appendChild(link); | |
loaded.push(file); | |
}); | |
break; | |
default: | |
// code | |
} | |
} | |
if (typeof ignored !== 'undefined' && ignored!= null && ignored.length > 0) | |
console.log(loaded, " have been loaded;", ignored, " have been ignored."); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Supports the following use for the client-side but not fully function for angularJS. So, bundling is still the applicable way.