adding category descriptions

bradpowellphotobradpowellphoto Registered Users Posts: 378 Major grins
I added this code to my javascript ao that I could add category descriptions to my site. I got one to work "events" but two of my custom categories just won't work. Any suggestions?

function addCategoryDescription() {
var categoryDescription = {
"My_Images_of_Vancouver_Island" : "from the surfers of Tofino to the lights of Victoria",
"Events" : "Parades, Rodeos, Bathtub Races and more!",
"Street_Scenes" : "from the mean streets of Vancouvers East End to Chinatown"
};

if ((YD.hasClass(document.body, "category")) && (!YD.hasClass(document.body, "subcategory"))) {

re = /category_(\S+)/i;
re.exec(document.body.className);

breadCrumb = YD.get("breadcrumb");
if (breadCrumb && categoryDescription[RegExp.$1]) {
divTag = document.createElement("div");
divTag.className = "categoryDescription";
divTag.appendChild(document.createTextNode(categor yDescription[RegExp.$1]));
breadCrumb.parentNode.insertBefore(divTag, breadCrumb.nextSibling);
}
}
if (YD.hasClass(document.body, "homepage")) {
re = /\>([\w\-]+)<\/a>/i;

divTag = YD.get("categoriesBox");
if (divTag) {
divTags = YD.getElementsByClassName("albumTitle", "p", divTag);

for (i=0; i<divTags.length; i++) {
re.exec(divTags.innerHTML);
if (categoryDescription[RegExp.$1] != undefined) {
pTag = document.createElement("p");
pTag.className = "categoryDescription";
pTag.appendChild(document.createTextNode(categoryD escription[RegExp.$1]));
divTags.parentNode.insertBefore(pTag, divTags.nextSibling);
}
}
}
}
}
YE.addListener(window, "load", addCategoryDescription);

Thanks!
“Look, I'm not an intellectual - I just take pictures.” Helmut Newton

My Vancouver Island Photography Website http://bradpowellphoto.com
My Facebook Page http://www.facebook.com/bradpowellphoto

Comments

  • devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited February 3, 2008
    Brad,

    The reason why the code works fine for "Events" and not "My Images of Vancouver Island" or "Street Scenes", is that second two have spaces in their title, while your categoryDescription array is looking for values with underscores.

    To fix this I would modify your code to the following...
    for (i=0; i<divTags.length; i++) {
      re.exec(divTags[i].innerHTML);
      var category = RegExp.$1.replace(" ", "_");
    
      if (categoryDescription[category] != undefined) {
        pTag = document.createElement("p");
        pTag.className = "categoryDescription";
        pTag.appendChild(document.createTextNode(categoryDescription[category]));
        divTags[i].parentNode.insertBefore(pTag, divTags[i].nextSibling);
      }
    }
    

    Cheers,

    David
    David Parry
    SmugMug API Developer
    My Photos
  • bradpowellphotobradpowellphoto Registered Users Posts: 378 Major grins
    edited February 3, 2008
    So this is what I have for code now. But it still is not adding descriptions to my custom categories. Any suggestions? Remember I am sinple and do not know any code <img src="https://us.v-cdn.net/6029383/emoji/ne_nau.gif&quot; border="0" alt="" >

    function addCategoryDescription() {
    var categoryDescription = {
    "My Images of Vancouver Island" : "from the surfers of Tofino to the lights of Victoria",
    "Events" : "Parades, Rodeos, Bathtub Races, Pageants and more!",
    "Street Scenes" : "from the mean streets of Vancouvers East End to Chinatown"
    };

    if ((YD.hasClass(document.body, "category")) && (!YD.hasClass(document.body, "subcategory"))) {

    re = /category_(\S+)/i;
    re.exec(document.body.className);

    breadCrumb = YD.get("breadcrumb");
    if (breadCrumb && categoryDescription[RegExp.$1]) {
    divTag = document.createElement("div");
    divTag.className = "categoryDescription";
    divTag.appendChild(document.createTextNode(categoryDescription[RegExp.$1]));
    breadCrumb.parentNode.insertBefore(divTag, breadCrumb.nextSibling);
    }
    }
    if (YD.hasClass(document.body, "homepage")) {
    re = /\>([\w\-]+)<\/a>/i;

    divTag = YD.get("categoriesBox");
    if (divTag) {
    divTags = YD.getElementsByClassName("albumTitle", "p", divTag);

    for (i=0; i<divTags.length; i++) {
    re.exec(divTags.innerHTML);
    var category = RegExp.$1.replace(" ", "_");

    if (categoryDescription[category] != undefined) {
    pTag = document.createElement("p");
    pTag.className = "categoryDescription";
    pTag.appendChild(document.createTextNode(categoryDescription[category]));
    divTags.parentNode.insertBefore(pTag, divTags.nextSibling);
    }
    }
    }
    }
    }
    YE.addListener(window, "load", addCategoryDescription);
    “Look, I'm not an intellectual - I just take pictures.” Helmut Newton

    My Vancouver Island Photography Website http://bradpowellphoto.com
    My Facebook Page http://www.facebook.com/bradpowellphoto
  • devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited February 4, 2008
    Hey Brad,

    Sorry there was a bug in my code, I have updated your version on your site...
    function addCategoryDescription() {
      var categoryDescription = {
        "My_Images_of_Vancouver_Island" : "from the surfers of Tofino to the lights of Victoria",
        "Events" : "Parades, Rodeos, Bathtub Races, Pageants and more!",
        Street_Scenes" : "from the mean streets of Vancouvers East End to Chinatown"
      };
    
      if ((YD.hasClass(document.body, "category")) && (!YD.hasClass(document.body, "subcategory"))) {
    
        re = /category_(\S+)/i;
        re.exec(document.body.className);
    
        breadCrumb = YD.get("breadcrumb");
        if (breadCrumb && categoryDescription[RegExp.$1]) {
          divTag = document.createElement("div");
          divTag.className = "categoryDescription";
            divTag.appendChild(document.createTextNode(categoryDescription[RegExp.$1]));
          breadCrumb.parentNode.insertBefore(divTag, breadCrumb.nextSibling);
        }
      }
      if (YD.hasClass(document.body, "homepage")) {
        re = /\>([\w\- ]+)<\/a>/i;
    
        divTag = YD.get("categoriesBox");
    
        if (divTag) {
          divTags = YD.getElementsByClassName("albumTitle", "p", divTag);
    
          for (i=0; i<divTags.length; i++) {
            re.exec(divTags[i].innerHTML);
            var category = RegExp.$1.replace(/ /g, "_");
    
            if (categoryDescription[category] != undefined) {
              pTag = document.createElement("p");
              pTag.className = "categoryDescription";
              pTag.appendChild(document.createTextNode(categoryDescription[category]));
              divTags[i].parentNode.insertBefore(pTag, divTags[i].nextSibling);
            }
          }
        }
      }
    }
    
    YE.onContentReady("bodyWrapper", addCategoryDescription);
    

    I changed your addListener to onContentReady, so the category descriptions are added quicker, otherwise it has to wait for all the images to load before they are added.

    Cheers,

    David
    David Parry
    SmugMug API Developer
    My Photos
  • bradpowellphotobradpowellphoto Registered Users Posts: 378 Major grins
    edited February 4, 2008
    mange takke! (many thanks! )
    David

    Thanks so much! That's perfect!

    Brad
    “Look, I'm not an intellectual - I just take pictures.” Helmut Newton

    My Vancouver Island Photography Website http://bradpowellphoto.com
    My Facebook Page http://www.facebook.com/bradpowellphoto
Sign In or Register to comment.