Created
May 2, 2011 21:27
-
-
Save yannickcr/952405 to your computer and use it in GitHub Desktop.
A simple JS snippet to execute your framework-dependant code when the (asynchronously loaded) framework is ready.
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
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Example</title> | |
<script> | |
// Define an empty array | |
var Ready = []; | |
</script> | |
<script async src="main.js"></script> | |
<script> | |
// Push a function in the array | |
Ready.push(function() { | |
// use the framework in main.js | |
}); | |
</script> | |
</head> | |
<body> | |
<!-- ... --> | |
<script> | |
// Push another function in the array | |
Ready.push(function() { | |
// use the framework in main.js | |
}); | |
</script> | |
</body> | |
</html> |
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
// Your framework | |
/* ... */ | |
// Overide the array push method to execute the futures functions immediately | |
Ready.push = function(fn) { fn(); } | |
// Execute the queued functions stored in the array | |
for (var i = 0, j = Ready.length; i < j; i++) Ready[i](); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment