Refresh a Google Adsense banner with Javascript

Often bloggers and site owners use galleries to have a greater number of pages, adding pages means adding clicks. Each…

November 9, 2013

Often bloggers and site owners use galleries to have a greater number of pages, adding pages means adding clicks. Each photo is a click, is a pageview. When you click next and the whole page is refreshed, with new photo, new link and… new banner impression. This is the goal: increase banner impressions.
Show more banner, have more pageviews, more time spent on your site and reduce bounce rate statistics (which bring better power when you sell banner).
From a user point of view I don’t like when this happens, but as a developer and as a owner of sites I understand the problem.
Google Adsense and other Ad-servers codes are written in javascript, so it’s not easy to refresh them with javascript and sometimes theri policies don’t want too.
A solution that worked for me is embedding their code in a local iframes and then refreshing that iframe with javascript.

Watch a demo here: demo refresh banner google adsense with javascript

Here is the html:

<a href="javascript:;" id='gobut'>refresh banner google</a>

<iframe src="refresh-banner-iframe.php" width="300" height="250"/>

The iframe file refresh-banner-iframe.php contains only the script from Google Adsense (or from your ad server). When you click on the refresh button… here is the jQuery function attached to the click event:

$(document).ready(function() {
	$('#gobut').click(function() {
		$url = $('iframe').attr("src");
		$('iframe').attr("src","about:blank");
		$('iframe').attr("src",$url);
	});
} );

There are different ways to refresh the content of an iframe. If the source on the iframe is in the same server as the container you can also use something like this:

document.getElementById(YourFrameID).contentDocument.location.reload(true);

But if the iframe points to a src on a different domain, you will receive a denied access error to the iframe’s contentDocument property by the same-origin policy.
So I’ve decided to work from the parent window, changing the url of the iframe and rewriting the same url.

You can use this trick to the next-previous commands of your gallery to refresh banners and stop realoding all the pages. Your time spent statistics will be better, your banner impression will increase, you site pageviews will be the same, but users will be happier.

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Recommended

Unobtrusive javascript to add an overlay banner

If you have to add an overlay to a site, here is a fast way to do it with an…

November 7, 2011

Decimal Degrees conversion and distance of two points on google map

Those two functions are usefull when you’re making Google Maps applications: When you show the coordinates of a point, it’s…

December 26, 2009

Add Internet Explorer class to body to detect old browser

When you have clients that still uses an old browser, you need to handle it. This javascript function detects the…

February 27, 2017

Modify list counter in :before pseudo class with jQuery using start attribute

Suppose you have two ordered list ol tags, and the second one has a start attribute. You also have in…

April 20, 2016

Social buttons: the fastest way for WordPress, without plugins

NOTE: the code in this post is written for WordPress but you can easily translate it in any language. You’re here…

September 15, 2015

Get Google Plus Follower count from PHP in WordPress

I’ve found a similar function to retrieve the number of followers of a Google Plus page, inside WordPress, but it…

May 7, 2014