Skip to content

Instantly share code, notes, and snippets.

@yannickcr
Created May 2, 2011 21:27
Show Gist options
  • Save yannickcr/952405 to your computer and use it in GitHub Desktop.
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.
<!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>
// 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