Ads requested via doubleclick often get served from an ad provider network that returns javascript that in turn performs document.write to place ads in the page. The use of document.write requires that the document be open, implying that the page hasn't reached document.complete. This gets in the way of deferring or lazy loading ad content. Putting such code at page bottom is helpful but doesn't do enough to lower all-important "page-loaded" time. Are "friendly iframes" the best we have? Is there any other alternative such as a clever way to override document.write that preserves the proper place in the dom?
Third party ads use document.write to add script and content into the "current" location in a page. The page owner doesn't have control over ad scripts and can't specify a display target div. The ad script expects to write and render at the document position where it is called, so it's not obvious how to position the ad correctly using deferred dynamic script loading. Lazy loading script into context is straightforward, but third-party content rendered in place via document.write is not so easily achieved.
I solved the problem with an iframe. I replaced the script tag with an iframe that points to a simple page on my server that contains the script tag.
I replaced the tag
with
adpage.htm simply contains:
I get used to render the ads at the bottom of the page in a hidden div and moving them with javascript.
something like this wherever you want to put your ads:
and at the end of the page:
postscribe.js by krux, as demonstrated at the HTML5 Dev Conference.
http://krux.github.com/postscribe/doc/postscribe.html https://npmjs.org/package/postscribe
I will give you another solution without using iframe. See http://github.com/shenjunru/LazyWrite
It will help you to defer the document.write()
The iframe method will work fine unless you are serving rich media ads. These are the type of ads that are generally flash based and, in some cases, expand out of their container. If you use iframes the rich media ads are restricted to the container (iframe).
The best way to accommodated all possible ads is to use the script method.
OK, so there are basically two primary ways in which an ad is rendered to the screen.
And honestly, the Javascript normally just renders an iFrame. Ad Networks want the iFrame b/c it gives them the easy ability to drop cookies (has this user seen this ad elsewhere on the network) and it's easy to drop impression pixels (method of counting that page was rendered).
So here's your best bet.
iframe.src = 'myurl?'; iframe.reload();
That should be all that you need to do. Set it up so that everything else loads and then load the advertising iFrames last.
Note that this may affect your revenue from the ads. It depends on the user experience, but if the ads don't load until the user has scrolled them off the screen, then you won't get clicks or make money.
Also, keep an eye on ad network performance. I know that these guys have pretty good response times, but there are lots of ad networks and sometimes even the big guys have crappy response times.