0

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!

5
  • What's the trouble with your script that it doesn't run when AdSense has been loaded? Shouldn't it be better to tackle that issue? Try to explain what doesn't work. Commented Mar 4, 2013 at 17:31
  • the script changes the content of a pre tag to table format (to be like the enviroment of a compiler). With AdSense, it changes nothing, Without, it works fine Commented Mar 4, 2013 at 17:39
  • Are there any errors in the JavaScript browser console? Do you have a live page where we can see what doesn't work? Commented Mar 4, 2013 at 20:02
  • Thanks Marcel Korpel! i didn't know chrome could be used like a debugger... And when I Ran the Element Inspector, I had discovered that the AdSense was having a connection Error, therefore, all the sripts AFTER it, they won't load. After a little research I've discovered another thing, the "window.onload" makes the function load AFTER the page is fully loaded. I solved the problem by putting the script tag in the end of the content and without the "window.onload" part and it runs smoothly! Thank you all for your patience with me Commented Mar 5, 2013 at 14:44
  • Fine. Now please answer your own question and accept it, so this question gets closed. Commented Mar 5, 2013 at 14:55

3 Answers 3

2

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');
}
Sign up to request clarification or add additional context in comments.

5 Comments

This is a little vague. Are you're talking about the OP's Javascript or the adsense?
I can't place the script before closing the body tag because it will place the Ad on the wrong place...
with that my script works fine but the Ad don't show... I can't put my pre with an ID because it in some pages it can have 1 pre tag and in others have 5 or more...
You can loop through and set the Adsense for each tag. This is a good starting point. You're not giving us enough code to work with so we have to make broad assumptions. Continuously coming back and saying "this won't work because of something else I haven't told you" is not helpful :)
Like I've said, I'm new to javascript, and I'm only using it because it will help me a lot on the layout... I really didn't know what to give and also i don't know if what I'm posting is useful... sorry for that :s
0

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.

2 Comments

I can't place the adsense in the end of my content because the ad's will appeard after of the all page...
then create a placeholder div or span and after your script done, inject the adsense code into it (innerHtml property) with javascript. this will load the script dynamically.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.