Writing a script in the DOM and running it before executing the DOM

My JavaScript (say, file 1) should get another JS file (say, file 2) from my server and complete it before the DOM renders. File 2 is actually a script declaration (e.g. AdSense) that uses document.write. I noticed that if I extract file 2 by creating a new script tag from file 1 and adding it to the node DOM head (using the DOM manipulation methods), the ad will correctly display in firefox, but not in IE

Research shows that scripts added to the DOM dynamically this way can be run AFTER the DOM is already displayed, in which case document.write overwrites the entire page. What are my options? I can think of it ... Which one is guaranteed to allow file 2 to manipulate the DOM before rendering?

1) Document.write script tag for file 2 from file 1 2) Synchronous Xmlhttp call (not sure if this blocks the DOM from rendering)

Reference:)

+4
source share
1 answer

Synchronous calls block the DOM from rendering.

If you want to not block, you can make an asynchronous call.

I would use document.write() because it executes the code then and there. not after loading.

+2
source

All Articles