Reputation: 85
I'm having a problem with my script because of the AdSense script... when the AdSense script fails to load, my script runs well, but if AdSense loads, My script doesn't load. And I know my script runs AFTER the AdSense script.
So I'm thinking, if my script runs before the AdSense script runs (because is a script to change a pre tag to a table, therefore it only changes the layout), everything will load, instead of just loading the AdSense...
My javascript is:
window.onload = function(){
var preElements = document.getElementsByTagName('pre');
var codeLine = new Array();
var newContent
for(var i = 0; i < preElements.length; ++ i)
{
var element = preElements[i];
newContent='<div align="center"><table width="75%" border="1" cellpadding="0" cellspacing="0" >'
codeLine = element.innerHTML.split('\n');
for(var j=0 ; j < codeLine.length ; j++){
newContent = newContent + '<tr><td width="30" class="codeNumber" >' + j.toString() + ' </td><td class="codeTab"> ' + codeLine[j] + '</td></tr>';
}
newContent = newContent + '</table></div>';
element.innerHTML = newContent;
}
}
It is loaded on the Head section and the AdSense is loaded inside a cell and I only have one adspace. I can't give an ID to cell because the AdSense isn't the only thing on the cell... And another thing.. The place where the AdSense is being called is completely different where i have the pre tag's
SOLVED: First I really didn't know much about this, and after a little research I've found the problem. 1º The AdSense was having a connection problem, and because of that all the scripts that runs after it, will not load 2º It doesn't matter where you have the script if you have "window.onload" in it... I thought that function worked when the window is loading but actualy, it will function after the window fully loads, and this is why it was creating a conflict with the AdSense.
You guys helped me see this things faster!
Upvotes: 0
Views: 569
Reputation: 85
SOLVED: First I really didn't know much about this, and after a little research I've found the problem. 1º The AdSense was having a connection problem, and because of that all the scripts that runs after it, will not load 2º It doesn't matter where you have the script if you have "window.onload" in it... I thought that function worked when the window is loading but actualy, it will function after the window fully loads, and this is why it was creating a conflict with the AdSense.
Upvotes: 0
Reputation: 77966
Generally most people put Ad and Analytics code includes right before the closing </body>
tag.
You should try to avoid race conditions with your code, from loading to execution it should be event driven and modular. If Adsense needs to go in a container that is being set by another script, it would make sense to have that other script load Adsense when it's finished updating your DOM, as a callback. Something like this:
function loadAdsense(elementId) {
var js = document.createElement('script');
js.src = 'http://pagead2.googlesyndication.com/pagead/show_ads.js';
window.google_ad_client = 'ca-pub-xxxxxxxxxxxx';
window.google_ad_slot = '1234567890';
window.google_ad_width = 336;
window.google_ad_height = 280;
document.getElementById(elementId).appendChild(script);
}
function preToTable() {
// set PRE to TABLE - id = myNewTable
// then load Adsense
loadAdsense('myNewTable');
}
Upvotes: 2
Reputation: 63
You should place your javascript in the head tag, and the AdSense code at the end of your content. Another solution is to use a javascript event to print the AdSense script when your first script is done.
Upvotes: 0