Page 1 of 1

Building Hyperlinks in ASP How to build hyperlinks in asp

#1 TheAKB  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-November 08

Posted 16 November 2008 - 02:49 AM

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 complex process 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

<%
MyHyperLink = "http://www.yoursite.com"
%>


Now to make a hyperlink on your ".asp" page we simply do this

<a href="<% =MyHyperLink %>"><% =MyHyperLink %></a>


or you could build the string and Response.Write it to the page if you like.

<% Response.Write("<a href=""" & MyHyperLink & """>" & MyHyperLink & "</a>") %>


To take this even further let's say your hyperlink info may not contain the "http://" info.
Here is a very simple example of testing for that and dealing with it.

<%
If InStr(LCase(MyHyperLink) ,"http://") = 0 Then
	 MyHyperLink = "http://" & MyHyperLink
End If
%>


<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 idea.


The next example is an email hyperlink.

<%
MyEmailAddress = "someone@somesite.com"
%>


To make an email hyperlink with that we simply do this.

<a href="mailto:<% =MyEmailAddress %>"><% =MyEmailAddress %></a>


or this

<% Response.Write("<a href=""mailto:" & MyEmailAddress & """>" & MyEmailAddress & "</a>") %>


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.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1