Flummox Flummox - 7 months ago 50
Javascript Question

adding HTML with Javascript, want to know when it is done loading

I am loading some HTML with Javascript, when that is done, I want to load some other script that makes use of the just loaded HTML. When I run both at the same time, there is a good change that the HTML is not ready before the Javascript, and so gives an error. The injected HMTL and JavaScript are created by someone else. And due to numbers, it's not feasible to do it by hand.

The reason I cannot use window.onload or others like it is that parts of the Page get replaced from the JS. Usually I just load the new stuff and then when the user interacts with it go to the next part.

Is this the right way to make sure the new HTML is done loading and then loading the new Javascript?

var rowDiv = document.createElement( "div" );
rowDiv.className = "row info-row";
rowDiv.addEventListener("load", addMoreJS(); );
// Add more HTML things
// Bring it All together.
domelement.appendChild( rowDiv );

if this question is not new and has a good answer, please point to it


You might want to change ->

rowDiv.addEventListener("load", addMoreJS(); );

To something like ->

rowDiv.addEventListener("load", addMoreJS);

Otherwise addMoreJS is getting executed before the load event even fires.