Forum Moderators: open
<!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.)
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(?)