Beta Dano archive script
First draft of a bit of Javascript to turn the “04/01/2003 – 04/30/2003” links that the beta preview of the new version of Blogger currently produces at this time into “April 2003” in modern browsers, incorporating what Stuart has taught me about unobtrusive DHTML (you just include a <script src=””> tag in your template, no other changes, and then it should either work or fail silently), while still retaining the charm of my incompetant programming style. Hidden away in the extended entry, in case there are children or other impressionable minds about.
Installation: copy the script below, save it as something useful like archivelist.js anywhere you like (your server, your Blog*Spot Plus account, any well behaved free host, which doesn’t include Geocities! with their horrid habit of delivering a popup along with a .js file), and then include it in your template with a <script src=””> tag, which you can put anywhere (<head>
or <body>
) you like. To make it as difficult on my test blog as possible, I put mine on a Tripod UK free site, and included it with
<script type="text/javascript" src="http://members.lycos.co.uk/philtests/archlist.js" defer="defer"></script>
in the <head> section, which says “no hurry about loading this, go ahead and display the page first if you like” so even if it’s completely inaccessible the page load isn’t delayed. The script is just
addEvent(window, "load", makePrettyArchives);
var archiveId = 'archive';
var month_name = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var dateRegEx = /\d\d\/\d\d\/\d\d\d\d - \d\d\/\d\d\/\d\d\d\d/;
// search for links with the archive ID that match the right pattern
function makePrettyArchives(){
if (!document.getElementsByTagName) return;
var links = document.getElementsByTagName("a");
for (var il=0;il<links.length;il++){
if (links[il].href.indexOf(archiveId)>-1 && dateRegEx.test(links[il].innerHTML)){
links[il].innerHTML = prettifyLink(links[il].innerHTML);
}
}
}
// for now, just turn the start month and year to a pretty month and the year
function prettifyLink(text){
startDate = new Date( text.slice(6,10), text.slice(0,2)-1, text.slice(3,5) );
text = month_name[startDate.getMonth()]+" "+startDate.getFullYear();
return text;
}
// Add an eventListener to browsers that can do it somehow.
// Originally by the fantabulous Scott Andrew.
function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}
and the only change you should need to make is to change var archiveId = 'archive';
if you call your archives something else. The 'archive'
part just needs to be something that’s in your archive URL, but won’t be in (most) other URLs, to save a little time and trouble. If your archives are in a separate directory, you can use the name of that directory, or you can use the main part of your archive filename (mine is archive.html, so I’m using archive). Then any link with that in the URL, which has contents matching the “04/01/2003 – 04/30/2003” pattern, will be rewritten to “April 2003” after the page loads. With a slow enough connection, you should be able to see it happening (which I think of as a feature, myself, because it looks cool to me). Works for me in Phoenix, er Mozilla Browser, IE6, and Opera, all on Windows. If it does or doesn’t work for you in something else, I’d like to hear about it. And if I’m doing something really stupid in the Javascript, I’d like to hear about that, too.
So, I thought, given how I got a namecheck, I’d have to cast my beady eye over the JS. And the only thing I could find that I’d change is that I’d define the
month_name
array as an array literal (i.e.,var month_name = ['January','February',...]
— theArray
object is so 1999 :)).I tell you, one day I will actually find something that I am better than everyone else at and then you will all TREMBLE BEFORE ME! Ahahaha!
Tight code, Phil. I almost wish I had a Blogger account so I could use it :)
Truth is, I’ve been copying and pasting that particular line for just a few days shy of two years now. I seem to have changed the variable name from mo_name (was I paying by the character?) at some point, but otherwise it’s as I typed it in May 2001. I don’t even see it anymore. I’m shocked to hear that it’s so 1999, since I thought it was at least 2000. Fixed, thanks.