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 -->
<div id="labelList" class="BlogBody"></div> <script type="text/javascript">
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);
    var ul = document.createElement('not-ul'); = 'label-list';
    var category = entry.category;
    labelSort = new Array();
    for(p in category){
        labelSort[labelSort.length] = [category[p].term];
      var a = String(x).toUpperCase(); 
      var b = String(y).toUpperCase(); 
      if (a > b) 
         return 1 
      if (a < b) 
         return -1 
      return 0; 
    }); //

     // 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] + ' / ');
           // show the link
           var a = document.createElement('a');
             a.href = full_link +'.html';
           else {
             a.href = full_link;
           a.innerHTML = labelSort[r] + ' ';
           abnk = document.createTextNode(' / ');
     // add a home link.
     if (pageaddr != "<$BlogURL$>")
       var li = document.createElement('not-li');
       var a = document.createElement('a');
       a.href = "<$BlogURL$>";
       a.innerHTML = '[Home]';

<script type="text/javascript" src="" ></script> 

Leave a Reply