Results 1 to 4 of 4

Thread: Clever Javascript alteration?

  1. #1
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Clever Javascript alteration?

    Ok I've got the code below from a site for generating a breadcrumb script for users based upon the directory they're in.

    However I've got some directories where there needs to be more than one word in the name and this causes problems because it looks a little untidy.

    So if the directory name is "Directory_A" would it be possible to code it so if an _ exists in the directory name, convert it to a space (ie %20)?

    Code:
    var crumbsep = " > ";
    var precrumb = "<span class=\"crumb\">";
    var postcrumb = "</span>";
    var sectionsep = "/";
    var rootpath = "/"; // Use "/" for root of domain.
    var rootname = "Home";
    
    var ucfirst = 1; // if set to 1, makes "directory" default to "Directory"
    
    var objurl = new Object;
    objurl['topics'] = 'All Topics';
    
    // Grab the page's url and break it up into directory pieces
    var pageurl = (new String(document.location));
    var protocol = pageurl.substring(0, pageurl.indexOf("//") + 2);
    pageurl = pageurl.replace(protocol, ""); // remove protocol from pageurl
    var rooturl = pageurl.substring(0, pageurl.indexOf(rootpath) + rootpath.length); // find rooturl
    if (rooturl.charAt(rooturl.length - 1) == "/") //remove trailing slash
    {
      rooturl = rooturl.substring(0, rooturl.length - 1);
    }
    pageurl = pageurl.replace(rooturl, ""); // remove rooturl fro pageurl
    if (pageurl.charAt(0) == '/') // remove beginning slash
    {
      pageurl = pageurl.substring(1, pageurl.length);
    }
    
    var page_ar = pageurl.split(sectionsep);
    var currenturl = protocol + rooturl;
    var allbread = precrumb + "<a href=\"" + currenturl + "\">" + rootname + "</a>" + postcrumb; // start with root
    
    for (i=0; i < page_ar.length-1; i++)
    {
      var displayname = "";
      currenturl += "/" + page_ar[i];
      if (objurl[page_ar[i]])
      {
        displayname = objurl[page_ar[i]];
      }
      else
      {
        if (ucfirst == 1)
        {
          displayname = page_ar[i].charAt(0).toUpperCase() + page_ar[i].substring(1);
        }
        else
        {
          displayname = page_ar[i];
        }
      }
      allbread += crumbsep + precrumb + "<a href=\"" + currenturl + "\">" + displayname + "</a>" + postcrumb;
    }
    document.write(allbread);

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'd just put this:

    Code:
    allbread=allbread.replace(/_/g, ' ');
    right before this:

    Code:
    document.write(allbread);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A note:
    Code:
    var pageurl = (new String(document.location));
    is far less efficient than:
    Code:
    var pageurl = document.location.href;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Then again not! I just tried it out and it will give the wrong href, so what you really need to do instead is change the 'displayname' here (addition red):

    Code:
    allbread += crumbsep + precrumb + "<a href=\"" + currenturl + "\">" + displayname.replace(/_/g, ' ') + "</a>" + postcrumb;
    Added Later - Important Note: I agree with Twey about location.href, I am disagreeing with my own earlier advice in this thread.
    Last edited by jscheuer1; 08-14-2006 at 08:57 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •