Monday, January 2, 2006

Blogger hack: reverse, year based archive list

This Blogger hack allows the monthly archive list, which might become very long if your blog, like mine, goes several years back, to be grouped by years. The listing will show the year of the current posting with expanded entries, and links to the other years. Clicking on another year will go to the latest posting page of that year, whith the monthly archive links expanded.

Additionnally, when looking at a previous post, the first link of the archive list allows you to go back to your most current posting.

<h2 class="sidebar-title">Archives</h2>
<ul class="archive-list">
<ArchivePage><li><a href="<$BlogURL$>">Publication actuelle</a></li></ArchivePage>
<script type="text/javascript">
var sCurrent=location.pathname;
if(sCurrent.indexOf("200")==-1){var dtNow=new Date();sCurrent=dtNow.getFullYear();}
else sCurrent=sCurrent.substr(sCurrent.indexOf("200"),4);
var archives=new Array();
<BloggerArchives>archives[archives.length]=new Array('<$BlogArchiveURL$>','<$BlogArchiveName$>');</BloggerArchives>
var sPYear="0000",bOpen=true,bClose=true;
for (var i=archives.length-1;i>=0;i--){
var sURL=archives[i][0],sYear=sURL.substr(sURL.indexOf("200"),4);
if(sYear==sCurrent){if(sYear!=sPYear&&bOpen){document.write("<li>"+sYear+"<ul>");bOpen=false;}document.write('<li><a href=\"'+sURL+'\">'+ archives[i][1].replace(sYear,"")+ '</a></li>');sPYear=sYear;}
else if(sYear!=sPYear){if(!bOpen&&bClose){document.write("</ul></li>");bClose=false;}document.write('<li><a href=\"'+sURL+'\">'+sYear+'</a></li>');sPYear=sYear;}
}
</script>
<noscript><BloggerArchives>
<li><a href="<$BlogArchiveURL$>"><$BlogArchiveName$></a></li>
</BloggerArchives>
</noscript>
</ul>