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…

Novembre 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

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

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…

Novembre 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…

Dicembre 26, 2009

The Quantcast CMP broke my sites

A javascript error in CMP blocks my site.

Maggio 19, 2023

Optimizing LCP, Largest Contentful Paint result

Notes about Core Web Vitals optimization challenge

Gennaio 4, 2023

Scroll to DIV by ID without jQuery

Use scrollIntoView instead of jQuery animate.

Dicembre 16, 2022