Forum Moderators: open

Message Too Old, No Replies

Dynamically inserting adsense

Using DOM, but it won't load...

         

johnnie

6:02 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To enhance user experience, move up my content and improve chances of my page fully loading, I want to defer the actual loading of an adsense block to the end of the document. To achieve this, I use the following construct:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="nl" xml:lang="nl">

<head>
<title>Dynamic adsense test</title>
</head>

<body>
<div id="adsense_here" style="width: 500px; height:500px;"></div>

<script type="text/javascript"><!--
var string = new Array();
var string2 = new Array();

string.push('<!--');
string.push('google_ad_client = \"pub-inseryourcodehere\";');
string.push('/* Blah blah */');
string.push('google_ad_slot = "adslotnumber";');
string.push('google_ad_width = 336;');
string.push('google_ad_height = 280;');
string.push('//-->');
var writestring = string.join('\n');

var adsDiv = document.getElementById('adsense_here');

adScript1 = document.createElement("script");
adScript1.type="text/javascript";
adScript1.text=writestring;
adsDiv.appendChild(adScript1);

adScript2 = document.createElement("script");
adScript2.type="text/javascript";
adScript2.src="http://pagead2.googlesyndication.com/pagead/show_ads.js";
adsDiv.appendChild(adScript2);
alert(google_ad_slot);
//-->
</script>
</body>
</html>

This is supposed to only add the adsense scripts after the page has completed loading, allowing the user to read my content even with adsense dead in the water. If you load this page into your browser (I'm using FF3.5.5), you'll see in the DOM explorer that all of the adsense scripts were properly added to the document tree. Also, in the alert box you'll see that at least the first script *did* get evaluated, since the variable "google_ad_slot" does have a value.

However, the ads simply don't appear... Can anybody help me? Why is it that the second script simply won't evaluate?

(NOTE: due to adsense restrictions the example might not work.)

rocknbil

7:18 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you try this?

Wrap all your inline stuff in a function, then execute it on window.onload().

<script type="text/javascript">
function loadAS() {
var string = new Array();
// etc, all of it
alert(google_ad_slot);
} // end function
window.onload=function() { loadAS(); };
</script>

Could be wrong, first guess is that the body and html closing tags aren't yet rendered, so it doesn't know it's complete(?)

johnnie

8:09 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rocknbill,

Thank you for reply. Unfortunately, that approach does little good. My example code already manages to insert the adsense code in the proper spot on the page. Somehow, it just won't render the ads. I suspect it has something to do with the fact that adsense relies on document.write().