Loading JavaScript Into Div Dynamically

前端 未结 6 1198
难免孤独
难免孤独 2021-01-22 15:32

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()

6条回答
  •  花落未央
    2021-01-22 16:17

    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).

提交回复
热议问题