Return To Home Page - Link to "The Wanker" - Contact Us


Return To Home Page

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 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, but in this example we just set it.

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


Now to make a hyperlink on our ".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>") %>

Either way, the result would be this.

http://www.CJWSoft.com

To take this even further let's say our 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>") %>

Which results in this.

someone@somesite.com

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.


Banner
ASP (Active Server Pages) is a technology developed by Microsoft. Pages using ASP are primarily developed in JScript, or VBScript and are integrated into the HTML of your Web pages. The ASP code is compiled on-the-fly by the server and the resulting output is standard HTML. By using ASP, Web pages can be dynamic, full of ever-changing content, and browser independent.
Active Server Pages asp search engine active server page asp application components tutorial CJWSoft ASPProtect ASPBanner ASPClassifieds www.aspclassifieds.com, www.powerasp.com,www.cjwsoft.com,www.aspphotogallery.com,www.codewanker.com,www.aspprotect.com,www.aspbanner.com,www.abay.com

 

Link to "The Wanker"
© 2007 CodeWanker - Active Server Pages Articles, Code Snippets, & Tutorials