Вы находитесь на странице: 1из 16

ASP Object

Objects are a way of encapsulating multiple methods (they're like functions) and variables in one easy to manage Uber-Variable (an Object). Objects in ASP resemble other Object Oriented Programming languages. In this lesson we will be using the ASP CDO.Message object as our example object to be dissected.
Advertise on Tizag.com

ASP Object Overview


Objects were created to combat the increasing complexity of programming. The rationale for understanding and using Objects in your programming is to make programming easier and your code more human readable.

ASP Create an Object - Server.CreateObject


An object in ASP is created by passing a name string to the Server.CreateObject function(actually referred to as a method). The string to create a Message object is "CDO.Message". We will be creating a CDO.Message object in this example. Note: Because objects are special there is a special way that you create and destroy them using the Set keyword. These areas are marked in red in the example below.

ASP Code:
<% Dim myObject Set myObject = Server.CreateObject("CDO.Message") 'You must Set your objects to "nothing" to free up the 'the computer memory that was allocated to it Set myObject = nothing %>

That wasn't too painful, was it? Let's cover some more bases on the object model. Objects are a collection of related things that are combined into this blob of programming goo that can be created and destroyed whenever we may need it. For example say that you wanted to make an object that allowed you to send an email... Well there are certain things all emails have: To, From, CC, Subject, etc. This list of variables that are common to every email would be quite tiresome to have to create for every email we sent. Wouldn't it be nice if we could create some sort of Uber-Variable(Object) that would group all these smaller variables into one thing?

ASP Object Properties


These smaller variables are commonly referred to as an object's properties and the format for setting these properties is nearly identical to setting a variable equal to a value. The correct syntax for setting an object's properties is:

objectName.propertyName = someValue

In this tiny example below we are creating a new mail object and setting its To and From properties.

ASP Code:
<% Dim myObject Set myObject = Server.CreateObject("CDO.Message") 'Then we set the To and From properties myObject.To = "little.timmy@example.com" myObject.From = "huge.jill@example.com" 'You must Set your objects to "nothing" to free up the 'the computer memory that was allocated to it Set myObject = nothing %>

Now I know we didn't DO anything in the above example, but we still need to learn a bit more about objects before we can get anything done! Objects, besides having a clump of associated common variables, may also have a collection of functions(which become referred to as methods) associated with them. These methods are processes that you would want to commonly do to either manipulate the variables of the object or to use the variables to do something. In our Messageobject we have a collection of information that, when put together into the proper email form and sent to an email service will become an email. All this complex code has been programmed by Microsoft employees and stored into the Message objects Send method.

ASP Object Methods


We cannot see the code that was used to program the Send method, but that's one of the great things about using object programming. You know what you need to know and nothing more. In our example below we create a Message object and set the necessary properties and send it off with the Send method.

ASP Code:
<% Dim myObject Set myObject = Server.CreateObject("CDO.Message") 'Then we set the To and From properties myObject.To = "little.timmy@example.com" myObject.From = "huge.jill@example.com" myObject.Subject = "Can you see me?" myObject.TextBody = "I'm really really big!" myObject.Send() 'You must Set your objects to "nothing" to free up the 'the computer memory that was allocated to it Set myObject = nothing %>

Note:If you are running this on your home computer there are a slough of issues that may arise with sending an email. Microsoft has a largeFAQ about using the CDO.Message object that may help you. Knowing Microsoft, that link may go dead soon, so Contact Us if it expires!

ASP Object Summary

In this lesson you learned how to create and destroy an object in ASP. You also learned how to access the properties and utilize the methods of an object. If you still have no idea what this lesson was about, hopefully there's enough information for you to hack out what you need to do in your ASP project.

ASP Components
An ASP Server Component is a collection of code that has been made by Microsoft (advanced users can also create their own components), and included in IIS. With the use of ASP you can unlock the power of this pre-made code.
Advertise on Tizag.com

These objects can be used to do a ton of things, such as: an easy-to-use ad rotation service, an interface to a database, a means of manipulating files and much more.

Using ASP Components


Making use of Microsoft's ASP Components in your ASP programming will allow you to do so much with ASP that you'll be kicking yourself for not using components earlier. You can access these built in components by creating objects of them. See our previous lesson if you need a refresher on what ASP Objects are. In this lesson we will be utilizing Microsoft's FileSystem Component to display all the files in our current directory. The first thing we need to do is to create a FileSystem object so we can use all the properties and methods that are in this component. Note: FSO stands for File System Object in this example.

tizagComponent.asp ASP Code:


<% Dim myFSO Set myFSO = _ Server.CreateObject("Scripting.FileSystemObject") Set myFSO = nothing %>

Accessing an ASP Component's Features


Once you have created an object of your desired component you can access all the methods and variables of that component. We have created an instance of the File System Component and stored it into myFSO. We need the folder that we're going to list all the files of and the GetFolder method of our FSO will do the job. Using the same directory we decided to use back in the Running ASP lesson we are going to set the path of this FSO to "C:\Inetpub\wwwroot\tizagASP\", the same directory that "tizagComponent.asp" was saved in.

Updated tizagComponent.asp ASP Code:


<% Dim myFSO, myFolder Set myFSO = _ Server.CreateObject("Scripting.FileSystemObject") Set myFolder = _myFSO.GetFolder("C:\Inetpub\wwwroot\tizagASP") Response.Write("Current folder is: " & myFolder.Name) Set myFolder = nothing Set myFSO = nothing %>

Display:
Current folder is: tizagASP

Finishing Up
The last thing on our to-do list is to get access to the names of the files in our working directory. The Folder object contains a method that returns a collection of all the files in the current directory. The code for accessing this collection and displaying the filenames is in the example below.

Updated tizagComponent.asp ASP Code:


<% Dim myFSO, myFolder Set myFSO = _ Server.CreateObject("Scripting.FileSystemObject") Set myFolder = _myFSO.GetFolder("C:\Inetpub\wwwroot\tizagASP") Response.Write("Current folder is: " & myFolder.Name) For Each fileItem In myFolder.Files Response.Write("<br />" & fileItem.Name) Next Set myFolder = nothing Set myFSO = nothing %>

If you have done every example in this ASP Tutorial your web page produced would look like this. Notice that the files are automatically sorted alphabetically!

Display:

firtscript.asp tizagComponent.asp tizagEmail.asp tizagForm.html tizagGet.asp tizagPost.asp

ASP Comments
As we have stated way too many times now, ASP uses VBScript as its default programming language. This means VBScript comments are ASP comments. It's a darn shame too because that means there isn't any support for multiple line comments that you see in HTML and various other programming languages.
Advertise on Tizag.com

ASP Comment Syntax: Single Line Comment


To create a comment in ASP you simply place an apostrophe in front of what you want commented out. The most common practice is to place the apostrophe at the beginning of the line of text like we have in the following example.

ASP Code:
<% 'Hello I am a comment Dim newVar1, newVar2

'Dim oldVar1, oldVar2 %>

In the above example we had two comments. The first comment was a note style comment. Programmers often use these kinds of comments to leave information to people who are going to read their code or to themselves. The second comment we created commented out "Dim oldVar1, oldVar2" preventing the ASP interpreter from reading this line of code. This kind of comment is a block-out comment that is often used to temporarily remove code from a file that may be causing errors or is just unnecessary at the time.

How Comments Fail in ASP


Besides not having any support for multi-line comments, your ASP comments will not work if you place them within a string. This is because the interpreter will think that your apostrophe is a part of the string and not a comment. Below is an example of this happening.

ASP Code:
<% Dim newVar1, newVar2 newVar = "Hello. 'I am not commented out" %>

ASP Special Characters


All the special characters that you see when programming ASP can be a little overwhelming at first. This lesson is aimed at providing a succinct look at the most common special characters that occur within ASP. This lesson will cover the following character/symbol combinations:
Advertise on Tizag.com

& ' _ : . <%...%> <%=...%>

That's pretty ugly and confusing to look at, so let's get right in to the details of each of the symbols and see what they mean.

String Concatenation: The Ampersand &


You can combine strings with the ampersand character. It acts as a glue between one or more strings to create one large string. See our Strings Lesson for a more detailed look at strings and string concatenation. Below is an example of the ampersand concatenating two strings.

ASP Code:
<% Dim myString myString = "One String"

myString = myString & " another string" Response.Write(myString) %>

Display:

One String another string

ASP Comments: The Apostrophe '


The apostrophe is used to prevent the ASP interpreter from executing the text that follows. In ASP there is only the single line comment. Check out our ASP Comments Lessonfor more information on comments. Below is an example of the apostrophe.

ASP Code:
<% 'This is a comment. %>

Spanning Multiple Lines: The Underscore _


Sometimes you can't fit all your ASP code on one line because your string is too long, you are tabbed over too far or you just want to break up the statement. With the use of the underscore you can tell the ASP interpreter that you line of code continues to the next line. This allows you to have a single ASP statement span multiple lines. In the following example we have such a huge piece of code we need to span over three lines.

ASP Code:
<% Response.Write("This is probably the longest "&_ "string to be typed out on this page and maybe "&_ "even this whole tutorial on ASP!!!") %>

Display:

This is probably the longest string to be typed out on this page and maybe even this whole tutorial on ASP!!!

Squishing Onto a Line: The Colon :


Sometimes you want to reduce the human readability of your code because you're either mentally disturbed or insane. The colon will help you satiate your crazy desires by letting you put multiple lines of ASP code onto a single line. Below is an example of how to make your code very hard to read!

ASP Code:
<% Dim Dim Dim x=3 %> x y z : y=25 : z=x-y : y=x*z : z=x*x-z+y : y=5*3*z*2/x

Calling Methods: The Period .


ASP allows for Object Oriented Programming and these objects have methods that can only be called by first stating the object, then placing a period and finally calling the method by its name. The form for using the period is:

myObject.MethodName()

If you would like to learn more about ASP objects see our ASP Object Lesson.

Declaring ASP Code: The Percentage %


ASP files are often made up of a combination of HTML, some other stuff and ASP. The tag that you use to stake out an area for your ASP code does not resemble normal HTML tags. Instead it is almost like just an opening tag that can be stretched very, very, very long. You must use this character sequence to insert ASP code into your ASP files.

ASP Code:
<html> <body> <% 'My ASP code goes here. %> </body> </html>

Write Shortcut: The Percentage Equal %=


The percentage equal special character sequence is a modified version of the standard ASP code marker. This modification allows for quick access to the Response.Write() method that is used to write information to the web browser. This shortcut can be used to quickly print numbers, strings, variables and anything else you might throw at it. Below is a few examples of using this shortcut.

ASP Code:
<%=2%> <br /> <%="Hello"%> <br /> <%=Date()%>

Display:

2 Hello 10/20/2011

ASP DLL Information


When creating or using complex ASP web applications you will surely run into one that requires the use of a DLL. The DLL usually includes code that was compiled in VB, C++, or other Windows programming languages to be used in the ASP application.
Advertise on Tizag.com

Registering a DLL

Before you can start using the code that resides in the DLL file, you must first register the DLL with the windows runtime library. There are two ways to let windows know about this new DLL.

or

Save the DLL to C:\Windows\system32\

Manually register it with the regsvr32 application

The first option is self-explanatory, but the second option takes a bit more work. Here is a quick HowTo on registering DLLs: 1. 2. 3. 4. Find out the location of your DLL. We will be using C:\Inetpub\wwwroot\tizagASP\myDLL.dll in this example. Click on the Start menu and choose Run Type in regsvr32.exe "C:\Inetpub\wwwroot\tizagASP\myDLL.dll" (With the quotes) You should get a confirmation message if it succeeded, such as: "DllRegisterServer in C:\Inetpub\wwwroot\tizagASP\myDLL.dll succeeded"

Using Your DLL in ASP


Now that you have registered your DLL you need to learn how to access the functions that are contained within it. Let us assume for the sake of this example that the DLL file is called "myDLL.dll" and the name of the class is "myClass". To create an instance of this class you would use the Server.CreateObject method like so:

ASP Code:
<% 'Note this is example code, it will not work ' unless you create a myDLL, myClass, and a myMethod Dim myObject myObject = Server.CreateObject("myDLL.myClass") myObject.myMethod("something") myObject = nothing %>

ASP ADO
This lesson will provide a brief overview of what ADO is and why it is necessary to have in your ASP programming repertoire. ADO stands for ActiveX Data Objects. ActiveX Data Objects are a collection of components that can be used in your ASP programs.
Advertise on Tizag.com

Accessing the Database


ADO is specifically used as means to communicate and manipulate a database. The types of databases ADO will allow your ASP program to interact include Access, MySQL, and MSSQL to name a few. In this lesson we will be connecting to an Access database, so you will need to have access to Access(hehe) to complete this lesson.

Create Your Access Database

Before you can connect to the Access database you have to first create the Access database file. Fire up your copy of MS Access and create the following database: 1. 2. 3. 4. 5. 6. 7. 8. Create a New blank database called "tizag.mdb" (without the quotes) and save it to "C:\Inetpub\wwwroot\tizagASP\" (without the quotes) Use the wizard to create the database. Add two fields to your table: FirstName and LastName Click Next Name the table "TizagTable" (without the quotes) Select "Yes, set a primary key for me" Click Next Click Finish

Now that we have our database all that remains to connect to it.

Using ADO to Connect to an Access Database


Create an ASP file called "tizagADO.asp" and save it in the same directory as your Access database file. In the following ASP code we first create an ADO database connection and then we set up the ConnectionString and finally we call the Open method with our Access database filename to finish the opening process.

ASP Code:
<% Dim myConn Set myConn = Server.CreateObject("ADODB.Connection") myConn.Open = ("DRIVER={Microsoft Access" &_ " Driver (*.mdb)};DBQ=" &_ "C:\Inetpub\wwwroot\tizagASP\tizag.mdb;") myConn.Close() Set myConn = nothing %>

ASP vs PHP
Here at Tizag.com we teach two ways to program dynamic web pages: ASP and PHP. Which one is right for you? Which one should you spend your precious time and resources learning? This lesson will talk about the benefits and drawbacks of both of these technologies and try to give you the direction you need for choosing one technology over the other.
Advertise on Tizag.com

PHP: PHP Hypertext Protocol


PHP has become a somewhat mature dynamic server side programming language over the past few years. As of writing this article PHP 5.x is the current release and millions of web pages are using PHP (we are at Tizag). PHP is a free technology you can download for many different Operating Systems (see our PHP Install Lesson for information on how to install PHP). Compared to ASP, PHP is very easy to pick up and learn a little at a time. PHP is an ideal language for the weekend or hobbyist programmer. Seems like all green pastures in the land of PHP.

However, businesses do not readily embrace PHP for many reasons. A great deal of companies are running operating systems such as Windows Server 2003 or one of the Window NTs, which have been optimized to run Microsoft's proprietary language ASP. Companies usually are reluctant to switch technologies when they already have a history with one type of technology. Such a transition requires retraining or even retraining much of their staff.

ASP: Active Server Pages


ASP is a technology that is included with Internet Information Services (IIS) which is included in Windows 2003, NTs and XP Professional. If you own XP Home Edition then you will need to pay a couple hundred dollars to upgrade to XP Professional before you can begin your ASP programming career. As far as programming languages go, ASP is definitely not as straightforward as PHP. The language has a plethora of confusing programming patterns that will take a while to learn. Besides that difficulty, there is also much less free information on the internet, preventing you weekend programmers from getting a quality education for no money down. On the other hand, ASP and ASP.NET are widely used in the business world. If you are looking to get a high paying job, ASP or ASP.NET would be a darn good start to improving your desirability to employers. This isn't to say that you couldn't get a job with PHP, because you can, but rather you would just have an easier time if you took the ASP path. A search on Monster.comof ASP vs PHP near a major city resulted with 47 jobs for PHP and 321 jobs for ASP.

ASP vs PHP: Conclusion


Are you looking for a job and have time and resources to learn a rather difficult technology? Learn ASP. Want to program in your spare time, as more of a hobby than a career? PHP will treat you just fine. There are plenty of exceptions to these conclusions, as they are more a suggestion then anything else. Hopefully, this will make your decision a little easier.

ASP Files
All file interactions in ASP are done through the File System Component that is included with IIS. It includes many objects that give you a window into your system's file system. Some of the more important objects include:
Advertise on Tizag.com

1. 2. 3.

File System Object File Object Folder Object

Before you can gain access the second and third objects we listed above you must create the grand daddy master File System Object (FSO). Through this overarching object, everything in the File System Component can be accessed.

ASP Creating the FileSystem Object


To create the FSO you simply create an object reference to it like any other object in ASP. For a quick review on ASP Objects check out our ASP Object Lesson. In the example below we create a filesystem object and destroy it.

ASP Code:
<% Dim myFSO Set myFSO = Server.CreateObject _ ("Scripting.FileSystemObject") Set myFSO = nothing %>

Using the File Object


To retrieve a File Object in ASP you must know the relative or complete file path to the desired file. In this example we will assume that a file exists at "C:\Inetpub\wwwroot\tizagASP\firstscript.asp". myFO stands for my File Object. This small script will get a file object and print out the filename using theName property.

ASP Code:
<% Dim myFSO, myFO Set myFSO = Server.CreateObject _ ("Scripting.FileSystemObject") Set myFO = myFSO.GetFile _ ("C:\Inetpub\wwwroot\tizagASP\firstscript.asp") Response.Write("The filename is: " & myFO.Name) Set myFO = nothing Set myFSO = nothing %>

Display:

The filename is: firstscript.asp

Using the Folder Object


To retrieve the Folder Object you must once again supply the relative or complete folder path. If you followed along with our How to Run ASP setup then you will have a folder tizagASP located at "C:\Inetpub\wwwroot\tizagASP\". myFolderO stands for my Folder Object. This example is borrowed from one of the previous lessons,
ASP Components and it will display a list of all the files in the "tizagASP" folder. We will once again be using

the Name property for both the Folder and File objects.

ASP Code:
<% Dim myFSO, myFolderO Set myFSO = Server.CreateObject("Scripting.FileSystemObject") Set myFolderO = _ myFSO.GetFolder("C:\Inetpub\wwwroot\tizagASP") Response.Write("Current folder is: " & myFolderO.Name) For Each fileItem In myFolderO.Files Response.Write("<br />" & fileItem.Name) Next Set myFolderO = nothing Set myFSO = nothing %>

If you have been following along with this tutorial start to finish your browser will display something like this when you execute the script.

Display:
The filename is: tizagASP firtscript.asp tizag.mdb tizagComponent.asp tizagEmail.asp tizagForm.html tizagGet.asp tizagPost.asp That's all we have for files in ASP for the time being. Look for a full coverage of ASP Files in the near future.

ASP Dates
This lesson will teach you how to use the ASP Date Function, how to convert an ASP Date to a String and how to format an ASP Date.
Advertise on Tizag.com

ASP Date Function


The ASP Date function has to be one of the easiest date retrieval methods of all time. To display the date on your page all you need to do is place the Date function as the Response.Write argument.

ASP Code:
<% Response.Write(Date()) %>

Display:
10/20/2011 You can even use some shorthand techniques to make printing out the current date to your web page only one line of ASP Code. See our ASP Special Characters Lesson for more information.

ASP Code:
<%=Date%>

Display:
10/20/2011 Pretty sweet.

Convert ASP Date to String


The conversion of an ASP Date to string is unnecessary. If you want to use the Date in a string simply concatenate it onto the string or use the write function as we have above and you're done.

However, if you want to use ASP to format a date into a specific form other than the default format of DD/MM/YYYY (D = day, M = Month, Y = Year) then you will need to use the FormatDateString function. This function is covered in the next section. If you want to convert a string to date format, check out our String to Date lesson.

FormatDateTime Function
The Format Date Time function takes two arguments: a date and (optional) an integer from 0 through 4. The meanings of these numbers are as follows:

0 - This is the default setting. A short date DD/MM/YYYY will be used. 1 - A long date defined by the computer's regional settings. 2 - A short date defined by the regional settings. 3 - (time)A time using the time format defined by the regional settings. 4 - (time)A time using military time HH:MM (H = hour, M = Minute)

Below is an example of all five used to format the Date function.

ASP Code:
<% Response.Write("0 = " & FormatDateTime(Date, 0)) Response.Write("<br />1 = " & FormatDateTime(Date, Response.Write("<br />2 = " & FormatDateTime(Date, Response.Write("<br />3 = " & FormatDateTime(Date, Response.Write("<br />4 = " & FormatDateTime(Date, %> 1)) 2)) 3)) 4))

Display:
0 1 2 3 4 = 10/20/2011 = Friday, October 21, 2005 = 10/20/2011 = 12:00:00 AM = 00:00

You'll notice that the last two options are pretty worthless if you are just working with a standard date, but they are useful for formatting ASP's Time.

ASP Hosting: SQL


If you are looking for a web host that supplies both support for ASP and for SQL, amongst other things, then check out our list of hosts we have gathered up. The list is broken up into three classes: Personal, Business, and Expensive Custom Solutions.
Advertise on Tizag.com

SQL and ASP Hosting: Personal


These web hosts and services are cheap and good for those who don't have a huge budget and just need minimal resources and support.

FreeSQL - This web site allows developers to practice their SQL for free. This is not a web host. DiscountASP.net - Cheap SQL Hosting. This is a shared web hosting environment.

SQL and ASP Hosting: Business


These web hosts are more expensive than the above hosts, but offer more features and powerful solutions for your business's needs.

Fortune City - Extra features for small businesses. Aschosting - 24 hour toll free emergency support line.

SQL and ASP Hosting: Custom Solutions


These web hosts are set up to provide a custom solution for your business and usually provide a dedicated server solution for your projects.

ICO - Dedicated Server Solutions. The Planet - Another option for a dedicated solution.

ASP Hosting: MySQL


If you are looking for a web host that supplies both support for ASP and for MySQL, amongst other things, then check out our list of hosts we have gathered up. The list is broken up into three classes: Personal, Business, and Expensive Custom Solutions.
Advertise on Tizag.com

MySQL and ASP Hosting: Personal


These web hosts and services are cheap and good for those who don't have a huge budget and just need minimal resources and support.

FreeSQL - This web site allows developers to practice their SQL for free. This is not a web host. Think Host - Cheap MySQL Hosting. This is a shared web hosting environment.

MySQL and ASP Hosting: Business


These web hosts are more expensive than the above hosts, but offer more features and powerful solutions for your business's needs.

ISP Server - MySQL only web hosting.

MySQL and ASP Hosting: Custom Solutions


These web hosts are set up to provide a custom solution for your business and usually provide a dedicated server solution for your projects.

We do not recommend MySQL for large businesses.

ASP Manual(s)
On this page we have collected a bunch of useful sites that provide a manual to ASP and VBScript, the language that you program ASP in by default. This page itself is not a manual. ASP is proprietary software of Microsoft and so we have split up information into Microsoft and Non-Microsoft related resources.

Advertise on Tizag.com

Microsoft

MSDN ASP Tutorial - Microsoft's ASP Tutorial under Windows 2000 Server Documentation Online. ASP to ASP.NET - This manual provides information on migrating from ASP to ASP.NET MSDN VBScript User Guide - Microsoft's VBScript User Guide. If you're going to be programming in

ASP you might want to get a strong grasp of VBScript, the default language to program ASP in.

Non-Microsoft

ASPIN - ASPIN has a huge collection of resources for ASP. The only downside is the time and

effort it takes to get what you want.

Encoding and Decoding URL strings


by Manohar Kamath August 31, 1999 When you pass something as an URL's querystring, the string is encoded and all special characters like space, %, etc. are converted into their %xx values. Here, xx denotes a number and the %xx represents any character that can not be passed within the URL in their original form. The characters include spaces, all punctuation symbols, accented characters (like , , etc.) and non-ASCII characters(Source: MSDN library). The URL has to be in one piece and symbols spaces can break it. Also, symbols like &, = and + are used within an URL to separate the querystring's key. So, if you want to pass a key-value pair something like Message=Hello World, the URL (when not encoded) may look like: Listing 1 mypage.asp?mesage=Hello World! and the space between "Hello" and "World" breaks the URL. So when you do something like Listing 2 <%=Request.QueryString("message") %> within mypage.asp, you will get only "Hello" The escape() function in JavaScript parses a string and converts and special characters into their %xx equivalents. This function is very useful when you are trying to "build" a URL from within your ASP page. So if you want to build a correct version of the URL shown in listing 1, you would do something like in listing 3 (which is in JavaScript): Listing 3 lsURL = "mypage.asp?message=" + escape("Hello World!") Alternatively, you can use the URLEncode function in the Server object to get the same results. Listing 4 is in VBScript. Listing 4 lsURL = "mypage.asp?message=" & Server.URLEncode("Hello World!")

The result is mypage.asp?message=Hello%20World%21 Decoding the querystring is easy - just call unescape() function in JavaScript. VBScript, on the other hand, does not have any in-built function to decode URLStrings. Note: You will need to call unescape() within the same page as you called escape() to decode the strings. Otherwise, a simple Request.QueryString("message") will automatically decode the URL for you. I wrote this tiny function so you can include it on any ASP page with any scripting language and use it. Just include it in any ASP page and call the function URLEncode() with a string argument. Listing 5 <script language=JavaScript RUNAT=SERVER> // This function decodes the any string // that's been encoded using URL encoding technique function URLDecode(psEncodeString) { return unescape(psEncodeString); } </script> An example of using this is in Listing 6 Listing 6 <%=URLDecode("Hello%20World%21") %> the result being - Hello World! Note: Some people have asked me how to decode those "+"s, which are nothing but encoded spaces in the URL. Although Request.QueryString("your_key") should remove the +s, here is the code that will replace +s with spaces. This is nothing but a modified Listing 5. Listing 6 <script language=JavaScript RUNAT=SERVER> // This function decodes the any string // that's been encoded using URL encoding technique function URLDecode(psEncodeString) { // Create a regular expression to search all +s in the string var lsRegExp = /\+/g; // Return the decoded string return unescape(String(psEncodeString).replace(lsRegExp, " ")); } </script>

Вам также может понравиться