views:

2515

answers:

4

Hi everyone, I'm having issues with google adsense and it loading before my jQuery and killing my codes, so I thought I'd try to append the Google Adsense javascript to the appropriate div using the document ready function, here's the code I'm trying to write:

<script language="javascript" type="text/javascript">
$(document).ready(function(){
    $(".googleBanners").html("<script language='javascript' type='text/javascript'>\n" + "google_ad_client = 'pub-8487967187298044';\n" + "google_ad_slot = '1088799521';\n" + "google_ad_width = 250;\n" + "google_ad_height = 250;\n" + "</" + "script>\n" + "<script language='javascript' src='http://pagead2.googlesyndication.com/pagead/show_ads.js' type='text/javascript'>" + "</" + "script>");
});
</script>

But I'm not so good writing javascript/jQuery so if someone could help me implement this that would be fantastic.

The error in FF I'm currently getting is "Error: google_protectAndRun is not defined". I'm not sure what that means, but I'm guessing I've written the jQuery code wrong.. lol

+2  A: 

You can't include external scripts that way.

To include javascript after the page has loaded, you should use jQuery's jQuery.getScript() function, but I don't know if that would work for Google Adsense.

A little more info can be found here:

http://geek.littleredstring.com/17-load-adsense-last-jquery

Philippe Leybaert
Thanks for the link, I tried that before coming here, for some reason the adsense code kills my other jquery scripts in IE.. :/ Will check out the getScript thing.. :)
SoulieBaby
A: 

I had this exact same problem. I eventually solved it by using jQuery to load an with a src pointing to an html file that contains the Google AdSense javascript. This is rather inelegant, since the Google AdSense code creates an <iframe>. And, I happen to be working with a Facebook application, so in my case, I've got an <iframe> (the Google Ad) in an <iframe> (the one I used to get around the Firefox error) in an <iframe> (my Facebook app). But, it works.

IkimashoZ
just a recommendation, learn how to use the post editing tools to highlight your HTML tags as code(as well as any code). then you're post will make sense :)
jaywon
+4  A: 

The way I do this is by having a placeholder on the spot I want the ad to appear.

<html>
   <body>
      <div id="googleadgoeshere"></div>
   </body>
</html>

Then place the google-code in a container at the end of the page.

<div id="adsense" style="display:none;">all the google javascript goes here</div>

I then use jQuery to move the iframe the adsense code creates once the page is done loading.

$(window).load(function(){
    $("#adsense").find("iframe").appendTo("#googleadgoeshere"); 
    $("#adsense").remove();
});

If you just try to move the #adsense div you will end up with a blank page. If you try to do this most any other way, you will end up with a blank page. Google had built in active ways to check that the code is not moved. If it is, your page will be blank. Why google has done this is beyond me, but I have found this workaround to work for me...

Danny Roodbol
A: 

Danny,

you are awesome...it worked like charm.

it worked for me in complex script.

sushil