Skip to content

Instantly share code, notes, and snippets.

@EddyRespondek
Created June 17, 2014 06:14
Show Gist options
  • Save EddyRespondek/ccdfd75e75ebebf7f676 to your computer and use it in GitHub Desktop.
Save EddyRespondek/ccdfd75e75ebebf7f676 to your computer and use it in GitHub Desktop.
Polls for element creation and then loads a new script. Could be used for just about anything including checking for external file loading. Note: there is also MutationObserver but couldn't find anything reliable. https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
var poll;
var timeout = 100; // gives up after 10 seconds
poll = function () {
setTimeout(function () {
timeout--;
if ( $('#myid').length === 1 ) {
var script= document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://mysite.com/js/myscript.js';
script.async = true;
document.body.appendChild(script);
} else if ( timeout > 0 ) {
poll();
} else {
// element failed to load
}
}, 100);
};
poll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment