Skip to content

Instantly share code, notes, and snippets.

@jochemstoel
Created December 8, 2015 12:01
Show Gist options
  • Save jochemstoel/e4bbf20c5f4e55729755 to your computer and use it in GitHub Desktop.
Save jochemstoel/e4bbf20c5f4e55729755 to your computer and use it in GitHub Desktop.
/*
* @package acquire
* @version 0.2
* @author Jochem Stoel (http://jochemstoel.github.io/)
* @url http://jochemstoel.tumblr.com/post/134786730189/javascript-browser-require-ajax
* @license Don't involve me.
*/
function acquire($lib, $fn)
{
var xhr, module, async;
module = { };
async = false;
if (typeof $fn == 'function')
{
async = true;
}
xhr = new XMLHttpRequest();
xhr.open('GET', '/' + $lib + '.js', async);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.onreadystatechange = function ()
{
if (xhr.readyState === 4 && xhr.status == 200)
{
if (async)
{
eval(xhr.responseText);
return $fn(module.exports);
}
}
};
xhr.send('lib='+$lib);
if (!async)
{
eval(xhr.responseText);
return module.exports;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment