Skip to content

Instantly share code, notes, and snippets.

@dsignr
Last active September 15, 2016 05:23
Show Gist options
  • Save dsignr/f3534f816a121f9694c61a1dc851a5b7 to your computer and use it in GitHub Desktop.
Save dsignr/f3534f816a121f9694c61a1dc851a5b7 to your computer and use it in GitHub Desktop.
Poll for jQuery until it is loaded
// Source: http://stackoverflow.com/a/17914854/381150
// Sometimes you don't have control over a web page and you need to inject some scripts which are dependent on jQuery.
// However, this will throw a jQuery/$ undefined error and so the only solution is to poll until it's available.
function defer(method) {
if (window.jQuery)
method();
else
setTimeout(function() { defer(method) }, 50);
}
//you can use it like
window.xyz = function(){
.... //same stuff as above
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment