2005-06-17

Non Javascript friendly way of hiding empty comments in Blogger

This is a simple non Javascript friendly way, or hack if you like, to hide empty comments in Blogger. I know that you can find other ways to do this. I found some that use a CSS rule to hide the comments and then use Javascript to detect and display them. In my version, when Javascript is turned off, comments will be visible.

The trick is to put your comments in any tag with an id attribute, in my case with the value comments. Then, directly after you've opened that tag, you have to insert some Javascript to hide it:

var comments = document.getElementById('comments');
comments.style.display = 'none';

Then you have to insert your comments, in my case a (semantic) definition list. After the comments you'll need to insert some Javascript to detect and show the comments. In my case by finding out if there were any dd tags.

if (comments.getElementsByTagName('dd').length) {
  comments.style.display = 'block';
}

Update: stupid me. There is a better way to check comments... just use <$BlogItemCommentCount$>.

if (<$BlogItemCommentCount$> > 0) {
  comments.style.display = 'block';
}

No comments: