Valery's Mlog

Mindlog of a Freak
February 10th, 2007 by Valery Dachev

AdSense Playground

There were lots of changes in my homepage in the past two months. I totally rewrote everything using Prado Framework and made it use the same template as my blogs (credits to GetTemplate). I almost reorganized the WordPress theme for this template and localized it, so I’m going to put it for download as soon as it’s stabilized. I localized almost all of my site, so I have full multi-language support…

… and that’s one of the reasons yesterday my homepage was finally approved for Google AdSense. Although I applied for AdSense for Content, I’m not fond on that as it’s seems like a complete waste of space. However, I’ve put an AdSense for Search in both themes (Prado and WordPress) as well as a referral link for “Firefox with Google Toolbar”. Thankfully Google have provided a way to easily integrate the results of AdSense for Search in the website.

In general adding AdSense for Search to the website is a simple task. However, a problem exists in cases when the search form happens to be nested into <form …> … </form> tags as it’s a form itself and these tags can’t be nested. This is usually a problem of Prado-based and ASP.NET-based websites where usually almost all of the content is put in a single form. If possible, you can take the search form out of the main form of the website. However I couldn’t do that. What I did was to create a new page containing only the AdSense for Search code in its body and put it in an iframe. This however requires some minor changes to the code provided by Google AdSense. I’ll discuss them right after the examples. Note that these are Prado examples, but ASP.NET developers would get the idea.

Here’s the iframe with the search box. Nothing special at all. You should just change the value of the “src” property to the URL of the page mentioned above:

<iframe name=”GoogleBox” src=”<%= $this->Service->constructUrl(‘GoogleBox’) %>”
frameborder=”0″ style=”width: 180px; height: 100px”>

And here goes the page. I’ve used a separate master page for boxes so I could use it for boxes, popups and so… but it could be a simple HTML page. Note I’ve removed the <com:TForm> and its closing tag from the master page (this is what it’s all about). I’ve also changed the target of the form – use “_parent” in order to display the results in the same browser page or “_blank” – to open a new one (“_self” would load the results in the iframe):

<%@ MasterClass=”Application.Pages.MasterBox” Title=”Google Box” %>
<com:TContent ID=”body”>
<!– SiteSearch Google –>
<form method=”get” action=”/” target=”_parent”>
<input type=”hidden” name=”page” value=”GoogleSearch” />
<!– … Google form goes here … //–>

Just one more tip: I didn’t want to break the overall design of my website, so I desided to make the results displayed as a part of my own website. So the SiteSearch code was generated having this in mind. I created such page with the required code and the address was “/?page=GoogleSearch”. However, note that the method should be “get”. If I had set the value of the “action” property to “/?page=GoogleSearch”, the parameter would be replaced with the parameters in the form. It’s a protocol feature and this is the way all browsers work. That’s why I’ve put this parameters as a hidden instead of adding it to the URL. This is something Google didn’t do for me although I’ve provided the action URL along with its parameter.

P.S.: I notices Google AdSense code is not a XHTML 1.0 Transitional. It is recommended to check it against W3C’s Validator.

P.P.S.: Sorry for not providing any links to the Google AdSense documentation but their website “… er ikke tilg?ngeligt i ?jeblikket.” :)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: