Alternate Ads and the noscript Tag

Posted By Darren Rowse 28th of April 2006 Adsense

This article by Eric Giguere is adapted from his upcoming e-book “Uncommon AdSense. ProBlogger readers will also find his contextual advertising blog of interest. Please Note that if you use the AdSense code outlined below that you should add your own publisher code and channel codes to it to make it your own.

AdSense and Chitika, like most ad serving programs, depend on JavaScript being enabled in the user’s browser. The code you paste into your web pages is JavaScript, so if the browser’s JavaScript support is disabled the code doesn’t run and no ads are displayed. Even your alternate ads won’t display — the alternate ad facility still requires the JavaScript code to run. Nothing will be displayed, in fact, if JavaScript is turned off. This is where the <noscript> tag becomes important.

How <noscript> Works

If JavaScript support is enabled, the browser executes any code it finds between a <script> and </script> pair and ignores any markup it finds between a <noscript> and </noscript> pair. If JavaScript support is disabled, however, the contents of all <script> tags are ignored and the contents of the <noscript> tags are displayed instead. Consider this simple page:

<html>
<body>
<script type=”text/javascript”>
document.write( “<b>Hello!</b>” );
</script>
<noscript>
<b>Goodbye!</b>
</noscript>
</body>
</html>

Load it into a browser with JavaScript enabled and you’ll see “Hello!” with JavaScript disabled you’ll see “Goodbye!” instead.

Using <noscript> with AdSense

Let’s say you’re using the 234×60 (half-banner) ad unit on your page and that you’ve defined an alternate URL for it. You’ve created a separate HTML file to hold your alternate ad, which could be pretty much anything that will fit in a box 234 pixels wide by 60 pixels high. Just make sure that the alternate ad doesn’t use any JavaScript.
Let’s say your AdSense code looks like this:

<script type=”text/javascript”><!–
google_ad_client = “pub-5964030199537728”;
google_alternate_ad_url = “http://www.mysite.com/mybannerad.html”;
google_ad_width = 234;
google_ad_height = 60;
google_ad_format = “234x60_as”;
google_ad_type = “text_image”;
google_ad_channel =””;
//–></script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>

Notice the alternate URL property: we’re telling AdSense to display the content at http://www.mysite.com/mybannerad.html if it has no ads to display. (These are all fake URLs, of course.) The equivalent Chitika code is very similar.

The alternate URL is the address of an HTML page much like this:

<!– mybannerad.html –>
<html>
<body>
<a href=”http://www.affprogram.com/myaffid”>
<img src=”affad.gif” width=”234″ height=”60″ border=”0″></a>
</body>
</html>

This particular alternate URL happens to display a banner promoting a product sold via an affiliate program; when clicked, the visitor is sent to the product site via an affiliate link.

So how does the alternate URL get displayed? An <iframe> tag like this one is used:

<iframe width=”234″ height=”60″ frameborder=”0″
src=”http://www.mysite.com/mybannerad.html”>
</iframe>

If you’re using an alternate URL already, why not make it do double duty and use it within a <noscript> tag as well? Here’s what I mean:

<noscript>
<iframe width=”234″ height=”60″ frameborder=”0″
src=”http://www.mysite.com/mybannerad.html”>
</iframe>
</noscript>

Now place the <noscript> block immediately after the AdSense code, like so:

<script type=”text/javascript”><!–
google_ad_client = “pub-5964030199537728”;
google_alternate_ad_url = “http://www.mysite.com/mybannerad.html”;
google_ad_width = 234;
google_ad_height = 60;
google_ad_format = “234x60_as”;
google_ad_type = “text_image”;
google_ad_channel =””;
//–></script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>
<noscript>
<iframe width=”234″ height=”60″ frameborder=”0″
src=”http://www.mysite.com/mybannerad.html”>
</iframe>
</noscript>

You can’t modify the AdSense code itself, of course, but it’s OK to immediately follow it (or precede it) with your own code. As long as your alternate ad doesn’t require JavaScript itself, you’ll always be showing some kind of ad to the user, regardless of their JavaScript setting.

Exit mobile version