Last active
December 15, 2015 19:18
-
-
Save safareli/5309991 to your computer and use it in GitHub Desktop.
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
(function(global){ | |
var inc = { | |
css:[], | |
js:[], | |
calls:[], | |
loadedJs:[] | |
}; | |
var loadData = function (obj) { | |
inc.calls.push(obj); | |
var head = document.getElementsByTagName("head")[0] || document.documentElement; | |
if (typeof obj.css == "string") { | |
obj.css = [obj.css]; | |
} | |
if (typeof obj.css == "object") { | |
includeCssFiles(obj.css); | |
} | |
if (typeof obj.js == "string") { | |
obj.js = [obj.js]; | |
} | |
if (typeof obj.js == "object") { | |
includeJsFiles(obj.js); | |
}else{ | |
obj.onload(); | |
} | |
function includeCssFiles (cssFiles) { | |
var stylesheet; | |
if (cssFiles.length === 0) { | |
return false; | |
} | |
for (var i = 0; i < cssFiles.length; i++) { | |
if (inc.css.indexOf(cssFiles[i]) != -1) { | |
continue; | |
} | |
inc.css.push(cssFiles[i]); | |
stylesheet = document.createElement("link"); | |
stylesheet.rel = "stylesheet"; | |
stylesheet.type = "text/css"; | |
stylesheet.href = cssFiles[i]; | |
head.appendChild(stylesheet); | |
} | |
} | |
function includeJsFiles (jsFiles) { | |
var script; | |
if (jsFiles.length === 0) { | |
obj.onload(); | |
return false; | |
} | |
if (inc.js.indexOf(jsFiles[0]) != -1) { | |
if (inc.loadedJs.indexOf(jsFiles[0]) == -1) { | |
setTimeout(function () { | |
includeJsFiles(jsFiles); | |
},1); | |
}else{ | |
includeJsFiles(jsFiles.slice(1)); | |
} | |
return; | |
} | |
inc.js.push(jsFiles[0]); | |
script = document.createElement("script"); | |
script.src = jsFiles[0]; | |
head.appendChild(script); | |
script.onload = function () { | |
inc.loadedJs.push(jsFiles[0]); | |
head.removeChild( script ); | |
includeJsFiles(jsFiles.slice(1)); | |
}; | |
//TODO add script.onerror | |
} | |
}; | |
global.__inc = inc; | |
global.loadData = loadData; | |
})(this); | |
//usage | |
loadData({ | |
css:['css/style.css','css/something.css'], | |
js:['js/jquery.js','js/some.js'], | |
onload:function () { | |
console.log('done') | |
} | |
}); |
ეს ანონიმური ფუნქციიდან მაქ ამოღებული და მაგიტო მქონდა ეგრე ^_^ დებაგინგისთვისაა და სადაც გინდა იქ შედებ :დ
@Stichoza დავფიქხე
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
მჯონი აჯობებს
inc
რომ რამენაირად ამოიღო Global Scope-დან. კონფლიქტების არიდების პონტში :>