Published: Sunday, June 18, 2000
Server-Side JScript Objects
By Richard Lowe
JavaScript (known as JScript in the Microsoft-specific version) offers the ASP developer an underused, alternative
scripting language with which to develop ASP pages. It's also a well-known language to the programmer coming
from a client-side scripting background. (Interested in learning more about JScript as a server-side scripting
language? Be sure to check out our articles on JScript,
the JScript Forum on ASPMessageboard.com or the
JScript ListServ on ASPLists.com!)
One particular feature of JScript which makes it attractive is its ability to create its own objects which can
be used in exactly the same syntax that COM objects created with Server.CreateObject() or VBScript
classes are. VBScript can be retained as the primary scripting language and still take advantage of the
features of JScript objects because objects can be passed to VBScript so that the programmer has the best of
both worlds!
The Basics
Functions are often used in both VBScript and JavaScript to encapsulate a piece of code to be used again and
again. The following JScript function is a version of the VBScript Trim() function, to remove
leading and trailing spaces from a string:
function jsTrim(strInput)
{
var strResult;
var objRegex = new RegExp("(^\\s+)|(\\s+$)");
strResult = strInput.replace(objRegex, "");
return(strResult);
}
|
Calling this function is simple; a string is passed to the function and a trimmed string is returned. There's
no need to re-write that function's logic ever again, however it must be included using a server-side
#include in every page it will be called from. (To learn more about regular expressions be sure
to check out the Regular Expression articles on 4Guys!)
Requirements such as the business rules layer of a multi-tiered application may be more complicated than can
be comfortably handled in a function. Circumstances may arise where building business logic into ASP is necessary.
In these situations JScript objects in ASP can serve as vessels to encapsulate logic or provide placeholders for
compiled components. JScript objects are all about breaking complex logic into discreet, manageable chunks
for the developer.
Example
Company XYZ is moving to an online helpdesk system, where tickets (problems) are called in and must be dealt
with. The requirements analysis shows the different ways that problem tickets can be manipulated. They can
be created, edited (status changes or notes added), escalated to higher-level support, or closed. There is also
a lot of information that goes along with a ticket: which user has reported the problem, the problem description and
severity etc.
Working with a ticket might mean writing a lot of code - and many variables (declared on each page) to hold
information temporarily. What if all the operations and data necessary for each ticket could be held in one
place? The remainder of the article shows the creation (and usage in VBScript) of one of the methods that
might be used in a helpdesk Ticket object.
First JScript Object
The process for creating your own custom object in JScript is the same client or server side. First, define the
functions you will use as methods. For our example the Ticket object will have a single function,
createTicket(). This function creates a new ticket by adding a row into a SQL Server
database table. The source code for createTicket() can be seen at the bottom of this page.
Although the workings of this function are explained with comments, it's important not to be
too concerned with how it works, but instead concentrate on the way it's integrated into the
Ticket object. Take a moment to review the source code below. When you are done, head over to
Part 2, where we'll look at how to encapsulate the createTicket()
function into an object.
Read Part 2
<script language='JavaScript' runat='server'>
// ~ The definition of the create method of our ticket object ~
// this function was designed to add a row to a table name 'tblTickets'
// in a SQL Server database with the fields userName varChar(100),
// description varChar(1000) and severity int and a System DSN named
// 'helpdesk'.
function createTicket()
{
// these two lines take care of escaping
// single quotes in the SQL statement
var objRegExp = new RegExp("'", "gi");
var strReplaceText = "''";
// the lines below reference 'this.' and then a property name
// these are the properties we will add to our object below
var strUser = this.user;
var strDescription = this.description;
var intSeverity = this.severity;
// this code inserts a new row into the MS SQL Server
// table 'tblTickets' and returns the @@ERROR code (0 if successful)
var strCommandText;
strCommandText = "INSERT INTO tblTickets (userName, description, ";
strCommandText += "severity)\nVALUES ('";
strCommandText += strUser.replace(objRegExp, strReplaceText) + "', '";
strCommandText += strDescription.replace(objRegExp, strReplaceText);
strCommandText += "', " + intSeverity + ")\n";
strCommandText += "SELECT @@IDENTITY AS tkt_id"
// here we escape any single quotes or SQL server will complain:
strCommandText = strCommandText;
var objConn = Server.CreateObject("ADODB.Connection");
// open our ADO connection and Execute the SQL command
objConn.Open("dsn=helpdesk", "sa", "");
objConn.Execute(strCommandText);
// Next return the error code, if any, to a variable. Notice how
// you can treat the connection object's Execute result as an
// implicit recordset object, using the GetString() method on
// the same line as the Execute method.
var returnErrCode =
objConn.Execute("SELECT @@ERROR as errorCode").GetString();
objConn.Close();
objConn = null;
// Return the error code
return(returnErrCode);
}
</script>
|