still messing around with my tag cloud

OK, I’m still messing around with my tag cloud code. As previously mentioned, I took a bit of code from this page, and have been messing around with it. I just added a couple of minor things that I wanted.
First, I now have a [Home] link at the end of the tag cloud, if we’re not actually on the home page. Second, I now change the link into just text if we’re on the page for a given label. None of this was a big deal, but it did serve to remind me of a few JavaScript basics, such as how to do RegEx matching in JavaScript, and how to get the URL of the current page. Here’s the code:

<!– originally taken from http://phydeaux3.blogspot.com/2007/05/automatic-list-of-labels-for-classic.html –>
<div id="labelList" class="BlogBody"></div> <script type="text/javascript">
//<![CDATA[
function listLabels(root){
var baseURL = '/search/label/';
var baseHeading = "My Tags";
var isFTP = false;
var llDiv = document.getElementById('labelList');
var entry = root.entry;
var h1 = document.createElement('h1');
h1.className = 'sidebar-title';
var h1t = document.createTextNode(baseHeading);
h1.appendChild(h1t);
llDiv.appendChild(h1);
var ul = document.createElement('not-ul');
ul.id = 'label-list';
var category = entry.category;
labelSort = new Array();
for(p in category){
labelSort[labelSort.length] = [category[p].term];
}
labelSort.sort(function(x,y){
var a = String(x).toUpperCase();
var b = String(y).toUpperCase();
if (a > b)
return 1
if (a < b)
return 1
return 0;
}); // http://www.java2s.com/Code/JavaScript/Language-Basics/CaseInsensitiveComparisonfortheArraySortMethod.htm
// where are we?
var pageaddr = document.location.href;
for (var r=0; r < labelSort.length; r++){
var li = document.createElement('not-li');
var full_link = baseURL + encodeURIComponent(labelSort[r]);
var re = new RegExp(full_link + '$');
if (pageaddr.match(re))
{
// just show the text
abnk = document.createTextNode(labelSort[r] + ' / ');
ul.appendChild(abnk);
}
else
{
// show the link
var a = document.createElement('a');
if(isFTP){
a.href = full_link +'.html';
}
else {
a.href = full_link;
}
a.innerHTML = labelSort[r] + ' ';
li.appendChild(a);
ul.appendChild(li);
abnk = document.createTextNode(' / ');
ul.appendChild(abnk);
}
}
// add a home link.
if (pageaddr != "<$BlogURL$>")
{
var li = document.createElement('not-li');
var a = document.createElement('a');
a.href = "<$BlogURL$>";
a.innerHTML = '[Home]';
li.appendChild(a);
ul.appendChild(li);
}
llDiv.appendChild(ul);
}
//]]>
</script>
<script type="text/javascript" src="http://www.blogger.com/feeds/13487006926024246136/blogs/3059692?alt=json-in-script&callback=listLabels" ></script>

view raw
tag-cloud-ex.html
hosted with ❤ by GitHub

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.