Last active
December 22, 2015 09:19
-
-
Save skamenetskiy/6451485 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* User: Semen Kamenetskiy | |
* Date: 05.09.13 | |
*/ | |
!function () { | |
'use strict'; | |
/** | |
* Global object container | |
* @type {*|window|{}} | |
*/ | |
var root = window || this || {}; | |
/** | |
* Document object container | |
* @type {*|HTMLDocument|{}} | |
*/ | |
var doc = root.document || {}; | |
/** | |
* Determines if the DOM is ready | |
* @type {boolean} | |
* @private | |
*/ | |
var __domReady = false; | |
/** | |
* Contains a list of scripts to load when the DOM is ready | |
* @type {Array} | |
* @private | |
*/ | |
var __domReadyScripts = []; | |
/** | |
* DOM ready method | |
* @param method | |
* @private | |
*/ | |
function __onDomReady(method) { | |
var eventName = 'addEventListener'; | |
doc[eventName] | |
? doc[eventName]('DOMContentLoaded', method) | |
: root.attachEvent('onload', method); | |
} | |
/** | |
* Attached new script to DOM | |
* @param {string} src | |
* @param {boolean} [async] | |
* @private | |
*/ | |
function __loadScript(src, async) { | |
var script = doc.createElement('script'); | |
script.setAttribute('type', 'text/javascript'); | |
script.setAttribute('src', src); | |
if (true === async) { | |
script.setAttribute('async', 'true'); | |
} | |
doc.getElementsByTagName('head')[0].appendChild(script); | |
} | |
/** | |
* JsLoader constructor | |
* @returns {JsLoader} | |
* @constructor | |
*/ | |
function JsLoader() { | |
this.name = 'JsLoader'; | |
return this; | |
} | |
/** | |
* JsLoader prototype | |
* @type {{load: Function}} | |
*/ | |
JsLoader.prototype = { | |
/** | |
* Loads the script when the dom is ready | |
* @param src | |
* @param async | |
* @returns {*} | |
*/ | |
'load': function (src, async) { | |
if (true === __domReady) { | |
__loadScript(src, async || false); | |
return this; | |
} | |
__domReadyScripts.push({ | |
src: src, | |
async: async || false | |
}); | |
return this; | |
} | |
}; | |
/** | |
* Setting global | |
* @type {JsLoader} | |
*/ | |
root['JsLoader'] = new JsLoader; | |
/** | |
* Attaching domReady event | |
*/ | |
__onDomReady(function () { | |
__domReady = true; | |
for (var i in __domReadyScripts) { | |
if (!__domReadyScripts.hasOwnProperty(i)) { | |
continue; | |
} | |
__loadScript( | |
__domReadyScripts[i].src, | |
__domReadyScripts[i].async | |
); | |
} | |
}); | |
}(); |
This file contains hidden or 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(){function f(a,c){var d=b.createElement("script");d.setAttribute("type","text/javascript");d.setAttribute("src",a);!0===c&&d.setAttribute("async","true");b.getElementsByTagName("head")[0].appendChild(d)}function g(){this.name="JsLoader";return this}var e=window||this||{},b=e.document||{},h=!1,c=[];g.prototype={load:function(a,b){if(!0===h)return f(a,b||!1),this;c.push({src:a,async:b||!1});return this}};e.JsLoader=new g;(function(a){b.addEventListener?b.addEventListener("DOMContentLoaded", | |
a):e.attachEvent("onload",a)})(function(){h=!0;for(var a in c)c.hasOwnProperty(a)&&f(c[a].src,c[a].async)})}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment