Home Grown Script Loader
I was wondering how RequireJS and AMD worked. So I created this snip. It might not be correct but it worked for me.
load('foo', function (foo){ var f = new foo(); f.a = 100; f.b = 200; alert(f.add()); }); function define(name, callback) { modules[name] = callback; } function load(file, callback) { var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true; script.src = file + ".js"; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script); script.onload = function() { callback(modules[file]); }; } var modules = []; // foo.js define("foo", function() { var foo = { a: 1, b: 2, add: function () { return this.a + this.b; } }; return foo; });
Home Grown Script Loader Read More »