In my last semester of study at college (Fall 2003) I was involved in a project that required that we have dynamic bread crumb trails for each of our pages. While searching across the net I found that there was really no good examples on how to do this except if you have a directory structure of how you wanted to trails to appear. Of course, this was not good enough for me, so I wrote a unique little bread crumb function that utilitizes cookies. This way it doesn't matter how your directory is setup you still get the crumbs on your pages.
For those of you not fimilar with bread crumb trails, they are a navigational tool that allows visitors to your site to know where they are at all times. A common example looks like this:
Home > Movies > Action > The Rock
First I suggest placing these functions in their own ASP page so that they are easily referrable to when you want to use them, but you can put the code of all pages if you choose to do so.
This is just general declaration and setting each variable so it may be used later in the code. The CachControl statement is there because in certain instances the caching of pages interferred with crumbs being set properly on the page. Request.ServerVariables("PATH_INFO") refers to the path from the URL bar you are current on, this is used to get the page name.
Code:
Public Function BuildCrumb(Title)
Response.CacheControl = "no-cache"
Dim Separator
Separator = " > "
Dim GetPage
GetPage = Split(Request.ServerVariables("PATH_INFO"), "/")
Dim PageLink
This portion builds the actual URL that you are currently on and sets your bread crumb text that is to be displayed on the page. The first part will build the URL and also grab any querystring values that are used to get to the specified location.
Code:
For Each var In Request.Querystring
PageLink = "<a href=" & GetPage(Ubound(GetPage)) & "?" & var & "=" & Request.Querystring(var)
Next
Set var = Nothing
If PageLink = "" then
PageLink = "<a href=" & GetPage(Ubound(GetPage)) & " class=crumb>" & Title & "</a>"
Else
PageLink = PageLink & " class=crumb>" & Title & "</a>"
End If
This is the meat of the function. This determines if the crumb has already been started. If contains a series of checks to ensure that duplicate titles are not in the crumb. This key if the user clicks the back button of clicks a link in the crumb to go back to a page they were once on.
Code:
If Request.Cookies("Crumb") <> "" Then
Dim GetCurrentPageTitle
GetCurrentPageTitle = Split(Request.Cookies("Crumb"), " > ")
If InStr(Request.Cookies("Crumb"), PageLink) = 0 then
Response.Cookies("Crumb") = Request.Cookies("Crumb") & Separator & PageLink
Else
For i = 0 To UBound(GetCurrentPageTitle)
If PageLink <> Trim(GetCurrentPageTitle(i)) Then
Crumb = Crumb & Trim(GetCurrentPageTitle(i)) & Separator
Else
i = UBound(GetCurrentPageTitle)
End If
Next
Response.Cookies("Crumb") = ""
Response.Cookies("Crumb") = Crumb & PageLink
Set Crumb = Nothing
Set i = Nothing
End If
Else
Response.Cookies("Crumb") = "<a href=index.asp class=crumb>Home</a>" & Separator & PageLink
End If
This last part sets the expiration date of the cookie and releases variables from memory.
Code:
Response.Cookies("Crumb").Expires = Dateadd("d", 1, Date)
Set GetCurrentPageTitle = Nothing
Set PageLink = Nothing
Set GetPage = Nothing
Set Separator = Nothing
End Function
This small function is used to reset the bread crumb on the site. For example if the user goes to another are of the site, you would want to restart the trail.
Code:
Public Function RestartCrumb()
Response.Cookies("Crumb") = ""
End Function
Now in your code, if you used an includes file (which I hope you did) you would start a new trail with this code:
Code:
<!--#include file="breadcrumb.asp"-->
<%
RestartCrumb()
BuildCrumb("Title") 'Title is what you want the page to be called
response.write(request.cookies("Crumb"))
%>
Or if you are wanting to use an exsisting trail:
Code:
<!--#include file="breadcrumb.asp"-->
<%
BuildCrumb("Title") 'Title is what you want the page to be called
response.write(request.cookies("Crumb"))
%>
I very much hope you find my breadcrumb function as useful as I have. Below is the complete code for easy reference.
Code:
'Code written by Ryan Wischmeyer, Indianapolis IN
'Copyright 2003
<%
'Function for creation of breadcumb trails
Public Function BuildCrumb(Title)
Response.CacheControl = "no-cache"
Dim Separator
Separator = " > "
Dim GetPage
GetPage = Split(Request.ServerVariables("PATH_INFO"), "/")
Dim PageLink
For Each var In Request.Querystring
PageLink = "<a href=" & GetPage(Ubound(GetPage)) & "?" & var & "=" & Request.Querystring(var)
Next
Set var = Nothing
If PageLink = "" then
PageLink = "<a href=" & GetPage(Ubound(GetPage)) & " class=crumb>" & Title & "</a>"
Else
PageLink = PageLink & " class=crumb>" & Title & "</a>"
End If
If Request.Cookies("Crumb") <> "" Then
Dim GetCurrentPageTitle
GetCurrentPageTitle = Split(Request.Cookies("Crumb"), " > ")
If InStr(Request.Cookies("Crumb"), PageLink) = 0 then
Response.Cookies("Crumb") = Request.Cookies("Crumb") & Separator & PageLink
Else
For i = 0 To UBound(GetCurrentPageTitle)
If PageLink <> Trim(GetCurrentPageTitle(i)) Then
Crumb = Crumb & Trim(GetCurrentPageTitle(i)) & Separator
Else
i = UBound(GetCurrentPageTitle)
End If
Next
Response.Cookies("Crumb") = ""
Response.Cookies("Crumb") = Crumb & PageLink
Set Crumb = Nothing
Set i = Nothing
End If
Else
Response.Cookies("Crumb") = "<a href=index.asp class=crumb>Home</a>" & Separator & PageLink
End If
Response.Cookies("Crumb").Expires = Dateadd("d", 1, Date)
Set GetCurrentPageTitle = Nothing
Set PageLink = Nothing
Set GetPage = Nothing
Set Separator = Nothing
End Function
'Reset the breacrumb trail to start another
Public Function RestartCrumb()
Response.Cookies("Crumb") = ""
End Function
%>