Created
November 5, 2011 18:56
-
-
Save 3rd-Eden/1341882 to your computer and use it in GitHub Desktop.
custom script loader
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 (global) { | |
var _namespace = global.namespace | |
, namespace = {}; | |
namespace.push = function push (fn) { | |
return fn && typeof fn === 'function' && fn(namespace); | |
}; | |
if (_namespace && _namespace.length) { | |
var i = _namespace.length; | |
while (i--) namespace.push(_namespace[i]); | |
} | |
}(this)) |
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 (a, b, c, d, e, f){ | |
for (d = c.length, f = a.getElementsByTagName(b)[0]; d--;) { | |
e = a.createElement(b); | |
e.src = c[d]; | |
f.parentNode.insertBefore(e, f); | |
} | |
})( | |
document | |
, 'script' | |
, [ | |
'http://cdn.example.com/core.js' | |
, 'http://cdn2.example.org/page.js' | |
] | |
) |
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
('namespace' in this ? namespace : (namespace = [])).push(function push (namespace) { | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rolling your own script loader
So what is wrong with the current script loaders:
So luckily I had the opportunity to design our code base from the ground up, so I knew I wanted to make it as fast and no-blocking as possible. My build system detects which scripts are depended on by different scripts and includes them in to a "core" file. I also assume that all widgets and page specific functionality will break if this is not included or available on the page when the script is executing.