HashOut: 2007/07/03

Placing AdSense Ads in the middle of Blogger posts

Hackman has an interesting hack to place AdSense ads right in the middle of your blogger posts. In this hack, Hackman places a HTML comment at the desired location in the post body while composing the post. Then using JavaScript he moves the contents before the HTML comment to a blank <div> tag before the AdSense code.

Hackman's hack is meant to work with the classic templates of Blogger. I have tweaked it a little to make it work for the new Blogger (layout) template. Here you go...

Find the <data:post.body/> tag in your new Blogger (layout) template and replace it with (Use copy-paste as some text below might be out of view):

<b:if cond='data:blog.pageType == "item"'>

<div expr:id='"adsensical" + data:post.id + "start"'></div>

<div style="margin:5px 0 -5px 10px">
<!--PLACE YOUR AD CODE HERE-->
</div>

&lt;div id="adsensical<data:post.id/>end">
&lt;div id="adsensical<data:post.id/>body">
</b:if>
<data:post.body/>
<b:if cond='data:blog.pageType == "item"'>
&lt;/div>
&lt;/div>

<script language="JavaScript">&lt;!--
//AdSensical Place Ads in Middle of Blogger Posts
//By hackman_3vilGuy http://adsensical-adsense.blogspot.com
//hackman {AT}caller {DOT}me {DOT}uk
//Modified for New Blogger (Layout) Template by Aziz (http://hashout.blogspot.com/)
topbit=document.getElementById("adsensical<data:post.id/>start");
bottombit=document.getElementById("adsensical<data:post.id/>end");
s=document.getElementById("adsensical<data:post.id/>body").innerHTML;
var regexIndex=s.search(/&lt;!--adsense-->/igm);
if(regexIndex>0) {topbit.innerHTML=s.substr(0,regexIndex);bottombit.innerHTML=s.substr(regexIndex+14);}
//-->
</script>
</b:if>


Now just replace <!--PLACE YOUR AD CODE HERE--> with your AdSense JavaScript code which you got from AdSense Setup and place <!--adsense--> anywhere within your post where you want the ad to appear (Make sure you insert it in Edit HTML mode).

The above code displays ads only on item (post) pages. If you want to display the ads on main or archive pages also, just remove the <b:if cond='data:blog.pageType == "item"'> and </b:if> tags. AdSense will automatically display only the first three ads on the page (in order of its appearance in the HTML) including the ones outside the posts section.

If you do not insert the HTML comment into your post content (HTML), then the ads will appear before the post body, but after the post title.

If you would like to see it working live before you begin implementing it on your blog, take a look here.

Related posts:
» Continue reading

Read more on , , ,

More reasons to drink tea

More healthy reasons to drink tea for health. Prevent memory loss, Alzheimer's. Polyphenols in black tea prevent tooth plaque, teeth cavity forming acids. Caffeine as good as ibuprofen for headaches.Scientists can't stop finding health benefits for tea. Tests at the University of Newcastle upon Tyne, UK, suggest that tea, and particularly green tea, may improve your memory and slow the onset of Alzheimer's.

A previous study in Chicago of people prone to tension headaches found that caffeine alone gave as much pain relief as ibuprofen. And a team in Illinois have found that polyphenols in black tea stop plaque forming on your teeth, and reduce the level of cavity-forming acids. » Continue reading

Read more on , , ,