views:

53

answers:

2

Hello, alot of web advertising providers use the old document.write method, in my case AdTech. Is it possible to overwrite the document.write method to defer after the onload event?

Kind regards

A: 

Moving all your scripts to the bottom of the page (before >/body<) with advert scripts absolutely last and not using domReady at all (don't need it if your scripts are put in after all HTML elements) will do the same thing. In fact, it might be possible that defering document.write calls can even ruin the entire page depending on how aweful the advert code is.

BGerrissen
Yes, right now I'm pushing out all the adcode at bottom of the page, and moving them to their respective container elements, but it's very slow :\
supermads
A: 

you can output a

document.old_write=document.write;
window._bufferedText='';
document.write=function(text) {
    window._bufferedText+=text;
}
//now let all the document.write go here
document.write('<h1>');
document.write('Hello World!');
document.write('</h1>');
//alert(window._bufferedText);
//end document.write methods
document.write=document.old_write;
document.write('<div id="deferred-write-placeholder"></div>');

window.onload=function() {
    document.getElementById('deferred-write-placeholder').innerHTML=window._bufferedText;
    delete window._bufferedText;
}

Notes:

  • you can rewrite something more complex to handle all instances of document.write code chunks.
  • you should replace window.onload with your javascript framework specific onload handler.
aularon
It's a step in the right direction, but the adcode can contain several document.write methods that again write out a script that uses document.write.
supermads