Monday, January 31, 2005

Reverse Archive Listings Revisited

It’s easy this way. You don’t have to collect the lines in an array and physically writeln() them into the page; instead, shuffle the elements of the list that has already been loaded.

Here’s how: right after your archive list in your template, which should be between <MainOrArchivePage> </MainOrArchivePage> tags in your sidebar, insert this code immediately before the closing </MainOrArchivePage> tag:

<script type="text/javascript">
<!--
var a=getElementsByClass("archive-list");
var l=a[a.length-1].childNodes;
for (var x=0; x<l.length/2; x++)
{
var n=l[x].innerHTML;
l[x].innerHTML=l[l.length-x-1].innerHTML;
l[l.length-x-1].innerHTML=n;
}
/***************************************/
/* Old getElementsByClass() DEPRECATED */
/* -- see new post above. */
/***************************************/
function getElementsByClass(nm)
{
var n=new Array(0);
with(document)
{
for(var i=0; i<all.length; i++)
if(all[i].className==nm)
n.push(all[i]);
}
return n;
}

-->
</script>

This should reverse the order of your archives, so that the most recent appears and the top, and the least recent at the bottom. And that’s how you reverse your archives!

Thursday, January 20, 2005

Smart Comment Counts

Ever looked at your comment count and asked, “1 Comments? What does that mean?” Well, there just happens to be a fix for that grammatical error; with a little bit of JavaScript and DHTML (DOM), you can make dumb counts a thing of the past. In fact, you can make your comment link say anything you want it to. I’ve chosen to put “Comment on this” in place of “0 comments”, for example.

Here’s how it’s done: right after the closing </Blogger> tag, insert this code:

<style type="text/css">

<!--
.hidden {display:none;}
-->
</style>
<MainOrArchivePage>
<script type="text/javascript">
<!--
var n=document.links;
for(var i=0; i<n.length; i++)
with(n[i])
{
if(innerHTML=="0 comments")
innerHTML="Comment on this";
else if(innerHTML=="1 comments")
innerHTML="1 comment";
}
-->
</script>
</MainOrArchivePage>
<ItemPage>
<script type="text/javascript">
<!--
var h=document.getElementsByTagName("H4");
with (h[h.length-1])
{
if(innerHTML=="0 Comments:")
className="hidden";
else if(innerHTML=="1 Comments:")
innerHTML="1 Comment:";
}
-->
</script>
</ItemPage>

Then republish your blog, and watch your counts get smarter!

Note: Some templates don’t disable the comment link on the Item Page. You can do that by searching your template for <p class="post-footer"> and then looking for the <BlogItemCommentsEnabled> ... </BlogItemCommentsEnabled> tags inside that paragraph. Simply place an opening <MainOrArchivePage> tag to the left of the opening tag, and a closing </MainOrArchivePage> tag to the right of the corresponding closing tag. Now your comment link will not display on a post page.