I wouldn't exactly recommend a JS based approach for this but if you are interested only in this option, the following algorithm/pseudo-code will help you:
Assumption: Your pages follow a strict Home > Search > Details structure and that the Details page cannot be called from anywhere but it's corresponding Search Page)
Is Page Type == Home
? If yes, breacrumb = "Home"
// This is because when it is Homepage, the breadcrumb is always just Home and no anchor needed because it will become a link to same page.
Is Page Type == Search
? If yes, breadcrumb = "<a href='home.htm'>Home</a>" + "Current Page Title"
// This is because your search page is always 2nd level and the first level is always home. Notice there is no link on the 2nd level because it actually is the current page.
Is Page Type == Details
? If yes, breadcrumb = "<a href='home.htm'>Home</a>" + <a href='prev_page_url'>Previous Page Title</a> + "Current Page Title"
//This is because we dont know what type of search page the previous page was. It could be Search Chairs or Search Tables or Search XX. Note again there is no link on 3rd level for reasons explained above.
So, in essence every page must be assigned a page type and based on that we decide what to show in the bread-crumb.
Now, let us see a sample:
- User is in Homepage. Page Type = Home so breadcrumb = Home.
- User clicks on Search for Chairs. In Search Chairs page, Page Type = Search, so breadcrumb would be Home > Search Chairs.
- User clicks on View Details. Page Type = Details, so breadcrumb = Home > Search Chairs (from previous page) > View Chair Details.
- User clicks on link to Search for Tables. Page Type = Search, so breadcrumb = Home > Search Tables. Previous page's breadcrumb is ignored.
- User clicks on View Details. Page Type = Details, so breadcrumb = Home > Search Tables (from previous page) > View Table Details.
- User clicks on Home link. Page Type = Home, so breadcrumb = Home. Previous page's breadcrumb is ignored.
This should overcome the issue that Sumurai8 had mentioned in this thread also.