How to tell JavaScript to execute a function after an element has been loaded?
Putting your script just above the body tag is a valid option. If the element in question supports it, you can also use it's onload event. You could also attach to the onload event of the window or document.
Well you can utilize the document.onload
and window.onload
methods
For example:
window.onload = function(){
//do some stuff
}
If you don't want to wait for the full page to load you can also poll for the element's existence:
function myFunc() {
if (document.getElementById('myElement')) {
// do stuff
} else {
setTimeout(myFunc, 15);
}
}