I need to load JavaScript code (which I don\'t have control over) from a URL into a div. The JavaScript code is a bunch of document.write() statements. Once the document.write()
How would I know when the document.write statements have finished executing and I can safely extract the text out of the div and use it?
Your approach won't work at all.
If the DOM has finished loading (which is probably has if you are starting to manipulate it programatically) then it will be in a closed state so calling document.write
will first call document.open
which will erase the entire document.
(If the DOM hasn't finished loading, then the content will be written to the end of the document and not to the point where the script is inserted).