What is the best way to lazy load doubleclick ads that use document.write?

别等时光非礼了梦想. 提交于 2019-12-03 00:07:47

postscribe.js by krux, as demonstrated at the HTML5 Dev Conference.

http://krux.github.com/postscribe/doc/postscribe.html https://npmjs.org/package/postscribe

OK, so there are basically two primary ways in which an ad is rendered to the screen.

  1. Using a Javascript include and some variables.
  2. Using an iFrame that you place on the page.

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.

  1. Figure out the iFrame URL that is being rendered. Some networks will tell it to you directly if you ask nicely.
  2. Place the iFrame on your canvas / html with the appropriate size (e.g. 728x90), but point it to a known URL on your domain that is blank. (or has a background color that matches your page).
  3. Add the javascript that updates the SRC of the iFrame at the appropriate time.

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.

I will give you another solution without using iframe. See http://github.com/shenjunru/LazyWrite

It will help you to defer the document.write()

Jokin

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:

<div id="destid" style="width:350px; heigh: 200px"></div>

and at the end of the page:

<div style="display:none">   
    <script type="text/javascript"> 
    document.write("<div id='srcid'>");
    //your doubleclickcode
    document.write("</div>");
    </script>
<script type="text/javascript"> 
    $("#srcid").appendTo("#destid");
</script>
</div>

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.

So there seems to be a standard forming around this called friendly iframes (FIF). It feels a bit like a hack but I feel better that the IAB is behind it.

Here's the documentation from the IAB: http://www.iab.net/media/file/rich_media_ajax_best_practices.pdf

It worked for me, and even though you are writing the js dynamically into an iframe, it still allowed the rich media ads to expand outside their space.

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

<script src="ad provider's url containing document.write()" type="text/javascript">
</script>

with

<iframe src="adpage.htm" style="border:none;width:...height:..."></iframe>

adpage.htm simply contains:

<html><head></head>
<body style="margin:0px;">
<script src="ad provider's url containing document.write()" type="text/javascript">
</script>
</body></html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!