Fix for slow-loading Google ads
categories: howto, javascript, troubleshooting
Google's AdSense ads, and lots of others, are added to pages using Javascript, and if that Javascript appears early in the page's HTML, it can seriously slow down the rendering of the rest of the page. That's because browsers generally refuse to do any further rendering of the page until they have a requested Javascript file in hand.
We ran into this with the ad in the left column of some pages on DailyStrength.org, which appears above the page content in the HTML; when we switched to a new ad provider, which required fetching multiple Javascript files, the issue was even more pronounced.
Since these ads only work when Javascript is enabled anyway, I decided to use some DOM manipulation (via jQuery, which is already on the page) to load the ad script in a hidden div at the bottom of the HTML, and then relocate the Javascript-generated iframe containing the ad to an empty, visible div where the ad needed to be:
$('#ad_hide').find('iframe').appendTo('#ad');
Now, loading the Javascript doesn't slow down the rendering of the content, and the ad appears right after the page is loaded.
Related posts: howto
- Track user clicks on certain links - June 3rd, 2008
- Fix uneven line lengths in headlines - January 12th, 2008
- Cycle through list elements with jQuery - January 7th, 2008
Related posts: javascript
- Could the brilliant people at Google please just solve this already? - June 6th, 2008
- Another cautionary remote Javascript tale - May 18th, 2008
- Remote Javascript with document.write() is killing me - April 11th, 2008
Related posts: troubleshooting
- HTML entities from AJAX into input fields using jQuery - December 19th, 2007
- jQuery IE7 "Operation Aborted" error - December 14th, 2007
April 11th, 2008 at 8:54 am
[…] Fix for slow-loading Google ads […]