Skip to content

Instantly share code, notes, and snippets.

@markselby
Last active August 29, 2015 13:57
Show Gist options
  • Save markselby/9622164 to your computer and use it in GitHub Desktop.
Save markselby/9622164 to your computer and use it in GitHub Desktop.
Running Javascript after render.
// Somewhere in your javascript assets included at the end of the document
$(document).ready(function() {
loaded = true;
doInit();
});
<!-- In the document head -->
<script>
var initFuncs=[], loaded = false;
function onInit(f) { initFuncs.push(f); if(loaded) doInit(); }
function doInit() { var f; while(f = initFuncs.shift()) { f(); }}
</script>
<!-- In an html template somewhere -->
<script>
onInit(function() { alert('My favourite cheese is ' + this); }.bind('Brie'); );
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment