spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / js / scripts / breadcrumbs

Sr. Programmer/Analyst
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume
Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next

How to Create a JavaScript Breadcrumb Script

By Lee Underwood

A breadcrumb is a navigational tool used to display the path to the current page. There are three types of breadcrumbs: path, attribute, and location. The path breadcrumb displays the actual route taken by the user to reach the current page. The attribute breadcrumb shows several different routes to the current page. The location breadcrumb displays the route from the home page to the current page, no matter how the user got there. Of these, the most popular method used is the location breadcrumb.

There are several different ways of displaying a breadcrumb. At WebReference, we use a basic trail created by hand with HTML code which is displayed at the top and bottom of this page. The code used is:

<a href="/">home</a> /
<a href="/js/">js</a> / <a href="/js/scripts/">scripts</a>
 / <a href="/js/scripts/breadcrumbs/">breadcrumbs</a>

JavaScript can also be used to display the breadcrumb trail on the current page. Once the code is added to the page, it will automatically determine the navigational trail for the breadcrumbs. Also, it won't need to be changed if the page is relocated in a different directory or on another server.

Of the many scripts available for creating breadcrumbs, we'll look at one created by Justin Whitford.

The script is as follows:

function breadcrumbs(){
  sURL = new String;
  bits = new Object;
  var x = 0;
  var stop = 0;
  var output = "<a href=\"/\">Home</a> &nbsp;>&nbsp; ";
  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> &nbsp;>&nbsp; ";
  }
  document.write(output + document.title);
}

It prints out a breadcrumb trail that looks like this:


The pointer between each link, ">", can be changed to something else, if desired. You will need to change the script on the red lines above. The pointer is shown in blue. If you want to change it to a double pointer, "»", change the first line displayed in red to:

  var output = "<a href=\"/\">Home</a> &nbsp;&#187;&nbsp; ";

Then change the next line displayed in red to:

    output += bits[i] + "/\">" + bits[i] + " &nbsp;&#187;&nbsp; ";

The breadcrumb trail would now look like this:


You can experiment with different pointers. You can also get rid of the extra spaces between the links by removing the non-beaking space characters (&nbsp;) on each side of the pointer.

The great part about this script is that it doesn't need to be configured. It will work exactly as it is. Click the "Select Script" button below and copy the script into a blank page. Be sure to use a text editor such as Notepad or NoteTab. Save it as a file called "crumb.js".


  Then use [Ctrl-C] to copy the text into your text file.

Next, you will need to link your Web page to the JavaScript file. In the head section of the Web page you want to display the breadcrumb trail — between the <head></head> tags, insert a link that looks like this: <script language="JavaScript" src="crumb.js"></script>. Be sure there are no spaces or code between the opening and closing <script> tags as this may cause the loading of the script to fail.

Finally, you will need to call the script. Place the following code where you want the breadcrumbs to be displayed:

<script language="JavaScript">
<!--
breadcrumbs();
 -->
</script>

That's it! Now, whenever the file is displayed, it will show the navigation trail from the home page to the current page. If you plan to use breadcrumbs, a good design technique is to use them through your entire site. If you run into any diffulties with this or other scripts, check out our JavaScript section and our forum.

Additional Links

home / js / scripts / breadcrumbs



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider

Created: January 31, 2005

URL: http://webreference.com/js/scripts/breadcrumbs/