Skip to content

Instantly share code, notes, and snippets.

@cameroncowden
Created September 27, 2017 17:56
Show Gist options
  • Save cameroncowden/c5c102b6b60c6411b7d66df7c5672a15 to your computer and use it in GitHub Desktop.
Save cameroncowden/c5c102b6b60c6411b7d66df7c5672a15 to your computer and use it in GitHub Desktop.
a vanilla javascript approach to running scripts when jquery is loaded in based on a custom event
<script>
//PRE (jquery usecase only)
//load jquery library, inline or externally using defer or aysnc
console.log('jQuery321 Ready! - version: ' + jQuery321().jquery)
$j = jQuery321.noConflict(); //map to new variable (extensible)
//THROW (include this after above in jquery file)
var myElement = document.querySelector('body');
var event = document.createEvent('Event');
event.initEvent('jq-rdy', true, true); //can bubble, and is cancellable
myElement.dispatchEvent(event);
//CATCH (use this wherever you want to do things after jquery is ready, without having to poll for it
document.querySelector('body').addEventListener('jq-rdy', function() {
// TODO event handler logic
console.log("caught something");
console.log($.fn.jquery); //this will let you know if a different version of jquery is being loaded and mapped to $
console.log($j.fn.jquery) //this is the jquery loaded from above
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment