Created
June 17, 2014 06:14
-
-
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
This file contains 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
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