-1

How can I display a breadcrumbs navigation menu from right to left?

e.g.:

document.title «myPage «Home

I am using the script below that displays left to right but I am unable to figure how to manipulate it correctly.

<script language="JavaScript">
<!-- 
  function breadcrumbs(){
    sURL = new String;
    bits = new Object;
    var x = 0;
    var stop = 0;
    var output = "<div class=topnav><A HREF=/>Hyperdisc</A> &raquo; ";

    sURL = location.href;
    sURL = sURL.slice(8,sURL.length);
    chunkStart = sURL.indexOf("/");
    sURL = sURL.slice(chunkStart+1,sURL.length)

    while(!stop){
      chunkStart = sURL.indexOf("/");
      if (chunkStart != -1){
        bits[x] = sURL.slice(0,chunkStart)
        sURL = sURL.slice(chunkStart+1,sURL.length);
      }else{
        stop = 1;
      }
      x++;
    }

    for(var i in bits){
      output += "<A HREF=\"";
      for(y=1;y<x-i;y++){
        output += "../";
      }
      output += bits[i] + "/\">" + bits[i] + "</A> &raquo; ";
    }
    document.write(output + document.title);
    document.write("</div>");
  }
 // -->
</script>

Which displays:

Home» myPage» document.title

j0k
  • 22,600
  • 28
  • 79
  • 90

1 Answers1

2

You don't need JS for this. What you need is direction CSS property. Here is the test case http://jsbin.com/welcome/55152/edit. Support for direction property is good

The main points are:

ul {
  direction: rtl;
}
li { display: inline; }
a { display: inline-block; }

UPDATED If you absolutely have to use JS for this here is the simple snippet:

var list = document.getElementById('nav'),
    lis = document.querySelectorAll('li'),
    l = lis.length - 1,
    i,
    newList = document.createElement('ul');

for(i = l; i >= 0; i--) {
  newList.appendChild(lis[i]);
}
list.parentNode.replaceChild(newList, list);

The snippet assumes markup like:

<ul id="nav">
  <li><a href="#">Home</a></li>
  <li><a href="#">myPage</a></li>
  <li>Document</li>
</ul>

Test case is here – http://jsbin.com/welcome/55163/edit

UPDATED for the actual snippet

When it comes to the actual code in the question you might want to replace everything from and including the last for() loop with this snippet:

document.write(output + document.title);
for(var i = bits.length - 1; i >= 0; i--){
  output += "&laquo; <A HREF=\"";
  for(y=1;y<x-i;y++){
    output += "../";
  }
  output += bits[i] + "/\">" + bits[i] + "</A>";
}
document.write("</div>");
spliter
  • 12,321
  • 4
  • 33
  • 36
  • The JS is dynamic. eg: if the user click thru pg2 from home it displays: Home>pg2 and then click to pg3 = Home>pg2>pg3 click back to home = Home> and then clicks to pg4 = Home>pg4 then clicks to pg2 = Home>pg4>pg2 CSS is hard coded –  Nov 29 '12 at 21:49
  • Excuse me, I might confuse the things, but I don't see how the dynamic nature of breadcrumbs is conflicting with CSS solution above. How to *build* the breadcrumbs is out of scope of "How To Display Breadcrumbs from Right to Left" question I think. CSS shows you exactly how to 'display from right to left'. But building the breadcrumbs is completely different thing. – spliter Nov 29 '12 at 21:57
  • @Andaero, I have extended the answer with the code snippet based on your example code. Please check it out. – spliter Nov 29 '12 at 22:04
  • Your example is a simple horizontal nav bar. When you click "Home", myPage should be removed. Your list is hard-coded; Imagine having to code out hundreds of links.... The JS creates the links dynamically when the user clicks thru a link and adds/subtracts the links from the breadcrumb trail as needed - A true breadcrumb nav bar –  Nov 29 '12 at 22:12
  • @Andaero I still believe that you mislead with your question. You ask about displaying but what you mean is building. There is nothing about clicking or any other event in your code. So, I think my answer totally fulfills the question "How can I display a breadcrumbs navigation menu from right to left?" even with JS and customizations of your actual code. The code I provide is not dependent on the way you build the breadcrumbs, so you can hardcode, you can make those dynamic you can do whatever you want. But anyway, I don't have much time to help with mislead questions, sorry. Good luck. – spliter Nov 30 '12 at 07:49