Tuesday, January 23, 2007:

a geeky non-musical post

Brief technical post for Beta Blogger users using Classic templates (this may also apply to the new templates; I couldn't say, really, as I've left off experimenting with them until they get their XHTML fixed):

If you're sticking with your classic template rather than the newer (non-validating XHTML) templates*, you might be wondering how to get an autogenerated link from the labels pages back to your home page.

Blogger doesn't seem to have a classic template conditional tag that will cover it. The accepted conditional tags for the classic template are MainPage, ArchivePage, ItemPage, and MainOrArchivePage. Blogger shows how to put a conditional tag on your header text (or image) to autogenerate a link from certain pages back to the main page (for, say, people coming in by search engine to a specific post).

Typically that's done with something like
<ItemPage><a href="<$BlogURL$>"><$BlogTitle$></a></ItemPage>

Since there isn't a classic template tag for labels, one option which seems like it would work is to change the default behavior to be to generate the header link and then to cause MainPage not to link it. Since you can't tell a specific page not to do something, the hack would be to tell MainPage to add an </a> before the header text or image and then to add another start href tag after it (to keep them paired and keep the HTML validating). For whatever reason, that doesn't work: Blogger treats label pages as still being at the main URL and so the link doesn't show up.

What does work is to use a bit of JavaScript to search the URL for the string "/search" and to add a link if that string is present. That code is below:
<script type="text/javascript"><!--
if (location.href.indexOf("/search")!=-1) document.write("<a href=\"<$BlogURL$>\">");
<script type="text/javascript"><!--
if (location.href.indexOf("/search")!=-1) document.write("</a>");

Of course this code won't work in Lynx or in browsers with JavaScript turned off, but according to this page, 95% of users do have JavaScript enabled.

Music post later today.

update: corrected a typo in the second If statement above which was preventing it from closing the tag.

update: modified this again to search the URL for "/search" rather than "search/label/", as the second one doesn't catch people going to a different page on the site through the search box at top left of blogs on *.blogspot.com.

*Blogger Beta bungles the XHTML validation even on otherwise-good classic templates, failing to encode ampersands in URLs, but two dozen errors is much better than 650 or more.

Labels: , , ,

Thanks for this. Can something similar be done to add a "older posts" link on the labels page (seems to display only 20.)

Also, I am missing an "older posts" link on my main page as well. Would you happen to know the appropriate classical template tag? I haven't been able to find it yet.


If there's a way to add pagination to labels pages, I haven't found it. Apparently Blogger is running on Apache Tomcat, and adding a ?max-results=# to your URL will change the display to show, instead of 20, whatever number you type in. But I've tried ?start-results= , ?offset= , ?skip= , ?skip-results= and a few others and nothing seems to work. If I could find the right command I could probably eventually code up something to make the browser add the pagination, but I can't seem to find it.

Google unhelpfuly implies only that you should switch to Beta which, if you care about having validating XHTML, you should not.

Post a Comment

<< Home