Loading Google Ad-Sense ads last with jQuery
I recently put a web site online (talkdb.net) that I built on Rails using jQuery. Of course Rails has lots of helper functions for Prototype but I found myself being more productive when using jQuery because it really helped me separate the concerns of developing the core interface and adding all the JavaScript AJAXy extras later. The decision to try using jQuery on this site was mostly inspired by reading about it’s use with Rails on this Err The Blog post.
I’m using a plugin that changes html radio buttons into a star rating form and adding the extra Google Adsense code delayed the execution of the classic $(document).ready(function(){}) to the point that the radio buttons would be visible while Adsense was loading ads. My first solution was to call the Google Adsense code with jQuery after doing all of the star-rating work. I made a file called ads.html and tried to load it with jQuery.get(‘ads.html’). When that didn’t work I tried loading the html with jQuery.getScript(‘ads.html’). Then I tried changing the html to JavaScript and using getScript and still had no luck. I searched some Google forums and found that people were generally unable to load GoogleAds using AJAX techniques.
Here is the solution I arrived at. I took all of my previous jQuery out of $(document).ready(function(){}) and created my own function called loadJS to house all of my jQuery interface stuff. Then I placed the Google snippet in my application.html.erb file before the