Skip to content Skip to sidebar Skip to footer

How To Deal With Document.write In A Script That's Added After A Page Has Loaded?

I'm in a situation where I need to add an advertisement script tag dynamically. The ad itself is just a simple script tag with the src attribute pointing to the ad server. The actu

Solution 1:

Here is something I have successfully done before

var oldWrite = document.write;
var wHtml="";
document.write=function(str) {
  wHtml+=str;
}
// load adcode
$('#somediv').prepend(wHtml);
// optionally reset
document.write = oldWrite;

This may fail if the adcode loads a script using document.write too. In that case use an iFrame since it will contain all the crap they can do


Solution 2:

If the document is loaded, document.write will flush the whole page. So it should not be put in a function which will be called after document loaded.

If you want more action, i think you can change to some other method.

http://javascript.info/tutorial/document-write some infomation about document.write is shown here. Hope it can be helpful.


Post a Comment for "How To Deal With Document.write In A Script That's Added After A Page Has Loaded?"