Building Hyperlinks in ASP
Often when using ASP or Active Server Pages you will find it necessary to
create Hyperlinks or email us links
dynamically. People seem to act like this is some sort of mystical and
and they couldn't be farther from the truth.
Like I keep telling everyone ASP is just dynamically created HTML and making a
hyperlink is accomplished by mixing HTML tags with the ASP variables. It's
very simple to make a hyperlink.
Here is an example where we have a variable that is a hyperlink. This
variable could come from a database query, but in this example we just set
MyHyperLink = "http://www.CJWSoft.com"
Now to make a hyperlink on our ".asp" page we simply do this
<a href="<% =MyHyperLink %>"><%
or you could build the string and Response.Write it to the page if you like.
(we even have a response.write
code generator that can help with that)
<% Response.Write("<a href=""" &
MyHyperLink & """>" & MyHyperLink & "</a>") %>
Either way, the result would be this.
To take this even further let's say our hyperlink info may not contain the
Here is a very simple example of testing for that and dealing with it.
If InStr(LCase(MyHyperLink) ,"http://") = 0 Then
MyHyperLink = "http://" & MyHyperLink
<a href="<% =MyHyperLink %>"><% =MyHyperLink %></a>
Basically we use the
LCase function to make the hyperlink we are testing lowercase just in
case "http://" was spelled using any capital letters. Then we use the
InStr function to see if the string "http://" is in the hyperlink. If it
is not we simply add it to it. In this case we are not taking into account
the possibility of a "https://" secure hyperlink, but you get the general
The next example is an email hyperlink.
MyEmailAddress = "firstname.lastname@example.org"
To make an email hyperlink with that we simply do this.
<a href="mailto:<% =MyEmailAddress %>"><%
<% Response.Write("<a href=""mailto:"
& MyEmailAddress & """>" & MyEmailAddress & "</a>") %>
Which results in this.
The thing to remember about ASP is that an understanding of HTML tags is
very important. A lot of people tend to put all their faith in their WYSIWYG
editors and have no idea how to write HTML by hand. Take some helpful advice
and learn HTML tags. You cant work with ASP and make anything worthwhile if
you can not understand and write basic HTML. Your WYSIWYG editors can only
take you so far.