How to tell javascript to execute a function after an element is loaded?

How to tell javascript to execute a function after loading an element through an external library?
Usually I have to bring a tag <script>after the element itself to work with DOM calls.

+5
source share
3 answers

Well, you can use methods document.onloadand window.onload For example:

window.onload = function(){
   //do some stuff 
}
+1
source

If you do not want to wait for the page to fully load, you can also poll the existence of the element:

function myFunc() {
  if (document.getElementById('myElement')) {
    // do stuff
  } else {
    setTimeout(myFunc, 15);
  }
}
+18
source

script body . , onload. onload .

+1

All Articles