I want to add an event when a DOM element is fully loaded.
The only difficulty is that the element does not exist yet when my script runs.
My workaround is to test at regular intervals whether the element is in the script
window.setInterval(function(){
if (document.getElementById("myId"))
/* do stuff */ ...;
}, 100);
Now comes another issue, the first time I find the element, am I guaranteed that it is fully loaded?
- If yes it is perfect.
- If no, how can I test it? (when it is not loaded yet, I can append it to the
onloadevent without any problem)
I don’t have jQuery.
Yes unless the element is an
<img>or an<iframe>which I believe it’s not, so it’s “perfect”.Another important note is:
You can attach events to an element though it is not fully loaded, the fact it’s in the DOM is sufficient for that task.
By the way, this is the difference between jQuery ready event versos javascript onload event.