Even though it's incredibly bad practice, most advertising servers still use document.write. It's terrible because placing <script type="text/javascript" src="http://www.ad-server.com/foo.js" />
where you want the ads to appear is page blocking.
I'm trying to move the script tag to the very bottom of the page (so at least everything above it loads), but then I need a way to have everything it outputs written to a specific place (preferably with jQuery selectors).
Basically what I currently have to work with is a dynamic third-party ad tag that was the result of an ad server call, saved as a string. Sometimes the string is HTML only. Sometimes it's a bunch of script tags. Sometimes it's a mix of embedded Javascript, HTML, and script tags. It could essentially be anything. Often times the script tags call document.write or call other tags which call document.write (don't we love daisy-chaining ad networks!)
If I were to just do $('#foo').html(string)
or $('#foo').append(string)
then that would work fine if the string only consists of HTML. But jQuery does something funky in the html() or append() functions when script tags are involved, and nothing is outputted.
I tried overwriting the document.write function which is eerily easy to do in Javascript, it seems. Unfortunately, that didn't work either for me (unless I did it wrong). After doing some research, it seems that a lot of people are having this same issue, and a potential solution was written at https://github.com/iamnoah/writeCapture and https://github.com/iamnoah/writeCapture/tree/writeCapture2 to capture document.write and solve the problem.
Unfortuantely, I found it mangling the output more often than not. so I'm looking for other alternatives.
MUCH TIA!!