• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Creating Custom Functions

By Chris Worfolk | on Apr 27, 2004 | 0 Comment
ASP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

Creating Custom Functions

A problem I have is that I have a lot of code doing the same thing. Recently, on a project I was doing, the URL’s had to be encoded to make them more friendly. It looked something like this:

lcase(Replace(url, ” “, “-”))

This would change a URL such as

/Books/This Book Title/

Into:

/books/this-book-title/

The problem is that this had to be done a dozen times in the script. At every point I wanted to do this, I had to include the same code over again. And then I ran into the problem of wanting to do more encoding. This mean’t I had to change every instance of the code.

The solution to this? Create a custom function. This can be put at the top of a page of code and can then be called from anywhere else on the page. Here is an example function:



Function Encode(url)

url = Replace(url, ” “, “-”)

url = lcase(url)

Encode = url

End Function

The basic syntax of a function is pretty simple. It starts with a declaration that this is a function then it is followed by the name of function, in this instance, I called it Encode. This is followed by the varies required for the function.

Here the variable is url but you can have as many as you need by stacking them up, such as:

Encode(var1, var2, var3)

The middle section is the actual processing, where the URL is encoded. The bottom bit of this is setting the end result. This is what will be passed back to where it was called from. Finally the function is ended.

Now you have your function, lets actually use it. Before the code would like:



<%

theurl = “/Books/This Book Title”

%>

<a href=”<%=(lcase(Replace(theurl, ” “, “-”)))%>”>Some link</a>

But now we can replace it with our little function:



<%

theurl = “/Books/This Book Title”

%>

<a href=”<%=(Encode(theurl))%>”>Some link</a>



And it will end up with the same result as before. But with less redundant code.

Share this story:
  • tweet

Author Description

Chris began web developing almost 10 years ago and since then has built up a network of websites including TheGurkin.com and IronCheesecake.com. He publishes all his articles on his web developer site CoolForge.com.

No Responses to “Creating Custom Functions”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,241Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.