Created
December 8, 2015 12:01
-
-
Save jochemstoel/e4bbf20c5f4e55729755 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
/* | |
* @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