Last active
September 15, 2016 05:23
-
-
Save dsignr/f3534f816a121f9694c61a1dc851a5b7 to your computer and use it in GitHub Desktop.
Poll for jQuery until it is loaded
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
// 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