Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Favicon 2

Status
Not open for further replies.

apex82

Programmer
Mar 2, 2009
127
GB
Hi,

I want to add a different coloured favicon depending on what area of the site a user is in

For example: Red for cars, blue for bicycles...etc

I want to achieve this with asp so I add an include file to all the pages then be able to determine the path to the page and from that display the correct favicon.

Code:
<!-- #include file="../favicon/favicon.asp" -->

This would save having to go through all the pages individually and typing in the favicon name.

Are there any similar examples out there that would give me an idea on this?

I’ve been looking, but haven’t found anything.

Thanks.
 
I'd structure my ASP as follows (and I always do this regardless)

very first line is a var that defines the page name (or ID)

Code:
<% strPageName = "about" %>
<!--#include virtual = "/global.asp" -->

The second line contains global functions that are used and you can take the pagename defined above it and do all sorts of things - including getting the favicon image - place it into a var

Code:
<%
urlFavicon = "default.png" ' default one to use
if strPageName = "about" then urlFavicon = "aboutFavicon.png"
if strPageName = "contact" then urlFavicon = "contactFavicon"
%>

^ use whatever method you need - even grab it from an XML file or databse - use a select case if you like...of course for dynamically generated pages, you may need to play around with the strPageName variable.

then in the <head></head> you can include something like this

Code:
<link rel="icon" type="image/png" href="<%=urlFavicon%">

Voila! Hope that helps....


TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
I'm trying to show the favicon and i'm able to do this locally by the following:

Code:
<%
	Sub Favicon()
		str_PathInfo = Request.ServerVariables("PATH_INFO")
		str_PathInfo = Replace(str_PathInfo, "/test/","")
		If InStr(str_PathInfo, "/") Then
			ary_PathInfo = Split(str_PathInfo, "/")
			str_Folder = "../images/icons/" & ary_PathInfo(0)
		Else
			str_Folder = "images/icons/root/"
		End If
		Response.Write "<link rel=""shortcut icon"" href=""" & str_Folder & "/favicon.ico"">"
	End Sub
%>

I am unable to get this to work live because the "test" is not part of the str_PathInfo.

Anyone any ideas, how I change this to make it work?

Thanks.
 
using vicvirk's methodology of naming a page (which is a great practice to follow), you could do this:

Code:
<%
pageName = "about"
%>
...snip...
<link rel="icon" type="image/png" href="/somepath/[COLOR=red]<%=pageName%>[/color].png" />

and create a favicon named for each of the pages you have - like cars.png, info.png etc.


If you wanted to get even more crazy, instead of naming the pages individually each time you create one (good if you have many pages), you can have it done automatically; just whack this in an asp page and use the #include file to have it generate your page name on the fly :]

Code:
'stick this in an asp page and use <!-- #INCLUDE FILE = "thisFile.asp" -->
arrPath = SPLIT(request.servervariables("SCRIPT_NAME"),"/")
getTop = cInt(UBOUND(arrPath))
getPage = SPLIT(arrPath(getTop),".")
pageName = getPage(0)
storePageName = pageName

this will always get the top page name (provided the querystring does not contain any periods in it, or forward slashes.)

there's probably an easier way to do get the page name (without the file extension), but this works just as well.

we have storePageName in there so we can restore pageName if you need to refer to it later.

you can also create a check for the use of a default icon if you have many pages that don't have a custom icon such as:

Code:
IF pageName = "index" OR pageName = "contactUs" THEN
    pageName = "default"
END IF
%>
<link rel="icon" type="image/png" href="/somepath/[COLOR=red]<%=pageName%>[/color].png" />
<%
IF pageName <> storePageName THEN
    pageName = storePageName
END IF
'restored pageName to the correct name for future use
%>
...

in the above section, we check to see if the name is one of the recognised ones for our default icon. then if they are, set pageName to "default", use the variable for the default icon, then change the pageName back to it's orginal state. but only if it is different to the stored name we retrieved from the arrays in the include file.

anyways, this is almost a bloody book. hope it's easy to follow, and more importantly, helpful!

________________________________
Top 10 reasons to procrastinate:
1)
 
Thanks g0ste.

I thought I might be able to adapt what I'd got so far since it worked locally just not live.

I include the following in each page:

Code:
<!-- #include file="../files/global.asp" -->
....
<% ShowFavicon() %>

and in the global.asp I have:

Code:
<%
    Sub Favicon()
        str_PathInfo = Request.ServerVariables("PATH_INFO")
        str_PathInfo = Replace(str_PathInfo, "/test/","")
        If InStr(str_PathInfo, "/") Then
            ary_PathInfo = Split(str_PathInfo, "/")
            str_Folder = "../images/icons/" & ary_PathInfo(0)
        Else
            str_Folder = "images/icons/root/"
        End If
        Response.Write "<link rel=""shortcut icon"" href=""" & str_Folder & "/favicon.ico"">"
    End Sub
%>

Shouldn't I be able to adapt this to work live?

When I look at the path string locally it is:

/test/bicycles/index.asp

When I look at the path string live it is:

bicycles/index.asp

I'm just not sure how to adapt what I have.

Thanks.
 
As far as I can tell;
Code:
<%
    Sub Favicon()
        str_PathInfo = Request.ServerVariables("PATH_INFO")
        [COLOR=red]str_PathInfo = Replace(str_PathInfo, "/test/","")[/color]
        If InStr(str_PathInfo, "/") Then
            ary_PathInfo = Split(str_PathInfo, "/")
            [COLOR=green]str_Folder = "../images/icons/" & ary_PathInfo(0)[/color]
        Else
            str_Folder = "images/icons/root/"
        End If
        Response.Write "<link rel=""shortcut icon"" href=""" & str_Folder & "/favicon.ico"">"
    End Sub
%>

the red line is not needed when you are live, as your path doesn't contain "/test/" (but this is not essential)

Testing this snippet on my server shows that the green line needs to be changed to the second element in the array (below) - as the path starts with a forward slash, and element(0) is blank.

Code:
str_Folder = "../images/icons/" & ary_PathInfo([COLOR=red]1[/color])

give it a try and see how you get on.

________________________________
Top 10 reasons to procrastinate:
1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top