Creating a breadcrumb using javascript

Breadcrumb is a dynamic navigation bar that usually is shown at top of page telling user on which page the user is currently on. The breadcrumb helps a user to track his/her current location on the website as well as to navigate to the previous pages. Here is a javascript code to create a breadcrumb:

// JavaScript Document
// Author: Shahbaz
  function GetBreadCrum()
  {
  
      var address = document.location.href;
      
      var breadcrumb = "You are here ";
      
      var arrAddress = address.split("/");
      
      for (var i=3; i<arrAddress.length-2; i++) 
      {
        var link = "";
        
        for(var j=0; j<i+1; j++){
        
          link = link + arrAddress[j] + "/";
        
        }
        
        breadcrumb = breadcrumb + "<a href=\""+link+"\">" + arrAddress[i] + "</a> >";
        
      }
      link = link + arrAddress[j] + "/";
      breadcrumb = breadcrumb + "<a href=\""+link+"\">" + arrAddress[i] + "</a>";
    
        return breadcrumb;

  }

The complete example can be downloaded from the following link: Breadcrumb

Add a Comment

Your email address will not be published. Required fields are marked *