Improve company productivity with a Business Account.Sign Up

x
?
Solved

Breadcrumbs javascript

Posted on 2007-12-03
7
Medium Priority
?
623 Views
Last Modified: 2008-02-21
I implemented this (shown below) javascript in my page , but it doesn't work how it is supposed to, the problem is this, lets say that user is on index page , and clicks another link, page2, and the navigation shows Index > Page2 , but when user clicks from Page2 to Index via <a href="index.php" the navigation on index shows Index > Page2 > Index , i want to show only Previous, not looping again...


var delim = '|';
var trailLength = 5;
var chunks;
var DAY = 24 * 60 * 60 * 1000;
 
function doCrumbs(){
  if(cookieTest('xxx')){
    crumbList = new CrumbList();
    if(getCookie('trailLinks')){
      var staleLinkCrumbs = getCookie('trailLinks').split(delim);
      var staleTextCrumbs = getCookie('trailText').split(delim);
      var startPos=
        (staleTextCrumbs.length<trailLength ||
        document.location==staleLinkCrumbs[staleLinkCrumbs.length-1])
        ?0:1;
      for(i=startPos;i<staleLinkCrumbs.length;i++){
        crumbList.add(staleLinkCrumbs[i],staleTextCrumbs[i]);
      }
    }
    if(document.location!=crumbList.links[crumbList.links.length-1]){
      crumbList.add(document.location,document.title);
    }
    setCookie('trailLinks',crumbList.links.join(delim),1);
    setCookie('trailText',crumbList.text.join(delim),1);
    crumbList.output();
  }
}
 
 
function CrumbList(){
  this.links=new Array();
  this.text=new Array();
  this.add = crumbListAdd;
  this.output = crumbListShow;
}
  function crumbListAdd(href,text){
    this.links[this.links.length]=href;
    this.text[this.text.length]=text;
  }
  function crumbListShow(){
    for(var i in this.links){
      if(i==this.links.length-1){
        document.write( ((i==0)?"":" > ") + this.text[i] );
      }else{
        document.write(
          ((i==0)?"":" > ")
          +"<a href='" + this.links[i] + "'>"
          + this.text[i] + "</a>"
        );
      }
    }
  }
 
 
function cookieTest(name){
  try{
    setCookie(name,'true',1);
    chunks = document.cookie.split("; ");
    return (getCookie(name)=='true');
  }catch(e){
    return false;
  }
}
 
function getCookie(name) {
  var returnVal = null;
  for (var i in chunks) {
    var chunk = chunks[i].split("=");
    returnVal = (chunk[0] == name)
      ?unescape(chunk[1])
      :returnVal;
  }
  return returnVal;
}
 
function setCookie(name, value, days) {
  if (value != null && value != "" && days > 0){
    var expiry=
      new Date(new Date().getTime() + days * DAY);
    document.cookie=
      name +"="+ escape(value) +"; expires="
      + expiry.toGMTString();
    chunks = document.cookie.split("; ");
  }
}

Open in new window

0
Comment
Question by:suprem3
  • 3
  • 2
6 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 20397957
What you are creating is essentially a history of the last few pages visited by the user, it isn't actually a breadcrumb trail. A breadcrumb trail relates pages on your website to other parent pages which have been predefined by you, regardless of the order the user visits them. You'd do that maybe by putting a special html comments header onto all your pages which specify the parent, and server-side code would open each file on the server and walk through the tree of files assembling an array of breadcrumb items.

But perhaps if you want to stick with what you have, you can do this:

In your crumbListAdd() function, instead of just appending the new data to the end of the arrays, first loop through the array searching for an identical item to the one you are adding. If you find it, truncate the array to that value. If you don't find it, append it to the end.
0
 

Author Comment

by:suprem3
ID: 20400252
This apparently is not an good script for my site....

I want to make an navigation like amazon have and a lots of other sites...

like this ... Home Improvement : Power & Hand Tools : Power Tools : Drills on top of the site, so the user will know where he is and to click any of these links to go back... can you help me where to find this ?
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 200 total points
ID: 20405363
Yes, that' s more like what you want. Breadcrumb trails are great on pages with multiple layers of pages like that.

Okay, the basic idea is you want each page to be assigned a predefined "parent" page, through whatever means you want. This should be defined before hand, probably hard coded by you. Each page you make you conceptually decide which page is it's parent.

Then, starting with the page you are currently on, you step through the parents like a linked list, building up the breadcrumb trail as you go. Once you're done, you output it to the screen in a somewhat attractive way.

My suggestion for you, if you want to do it entirely in javascript, is you store the "Page hierarchy" in a single .js file which you import into all your pages. the hierarchy is stored as a series of "names",  "index to the parent", and "url of thi spage" items, giving you enough informatio nt obuild up the breadcrumb trail. If the index to the parent page is -1, then we assume it doesn't have a parent and we stop.

The function that builds up the breadcrumb array is also responsible for formatting

Here's a starting stub for you to work from:




////////////////// BREADCRUMB.JS BEGINS ////////////////
 
var pages = new Array();
var breadcrumbtrail = new Array();
 
// FORMAT:   "Page Title|index of parent, or -1|url to this page"
 
pages[0] = "Welcome|-1|index.htm";
pages[1] = "Home Improvement|0|homeimprovement.htm";
pages[2] = "Power Tools|1|powertools.htm";
pages[3] = "Drills|2|drills.htm";
 
 
function makeBreadCrumbTrail(startindex) {
    var page;
    page = pages[startindex].split("|");   
 
    while( page[1] > -1 )
    {
        breadcrumbtrail[breadcrumbtrail.length] = "<a href=\"" + page[2] + "\">" + page[0] + "</a>";
 
        page = pages[page[1]].split("|");
    }
}
 
 
////////////////// BREADCRUMB.JS ENDS ////////////////
 
 
 
 
 
 
 
 
////////////////// DRILLS.HTM BEGINS ////////////////
 
<script src="breadcrumb.js"></script>
 
<script>
 
// We pass in the starting page. Also hard coded.
makebreadcrumbtrail(3);
 
// Now loop through breadcrumbtrail[] array, in reverse, and print out the trail
for(var i=breadcrumbtrail.length-1; i>=0; i--) {
 
    // put your printout code here
 
}
 
</script>
 
 
/////////////////////////////////////////////////////

Open in new window

0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 

Author Comment

by:suprem3
ID: 20407992
I found another one , in php which works great but anyway tnx for trying to help me , maybe i should give the code for other users...
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 20408211
yeah if you found your own php script that did it, better would be to post the code/link you found and request to close question with refund. my code above is an untested stub anyways :P
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20953154
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

By following these Magento e-commerce development tips, you can increase your website's conversion and profitability. Read this post for more details.
These seven tips can help you create an extraordinary website, one that captivates audiences and has them wanting to return regularly for more. Keep reading to find out what your site is missing and what you need to add!
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question