Following on from the first three parts of my “Using JavaScript Operators” in ColdFusion 8 series, where I introduced Arithmetic, Assignment and Logical Operators, it is now the turn of Comparison Operators.

In the past we have been restricted to the more ‘wordy’ operators such as those defined in the first table below.

Operator Description
IS
EQUAL
EQ
Perform a case-insensitive comparison of two values. Return True if the values are identical.
IS NOT
NOT EQUAL
NEQ
Opposite of IS. Perform a case-insensitive comparison of two values. Return True if the values are not identical.
CONTAINS Return True if the value on the left contains the value on the right.
DOES NOT CONTAIN Opposite of CONTAINS. Return True if the value on the left does not contain the value on the right.
GREATER THAN
GT
Return True if the value on the left is greater than the value on the right.
LESS THAN
LT
Opposite of GREATER THAN. Return True if the value on the left is smaller than the value on the right.
GREATER THAN OR EQUAL TO
GTE
GE
Return True if the value on the left is greater than or equal to the value on the right.
LESS THAN OR EQUAL TO
LTE
LE
Return True if the value on the left is less than or equal to the value on the right.

However, with the arrival of ColdFusion 8, the CFML scripting language has been brought into line with other major scripting languages. This change is undoubtedly good for ColdFusion as developers familiar with the ActionScript and JavaScript syntax can now more effectively code ColdFusion and vice-versa.

The following table describes the comparison operators that can be used in ColdFusion 8, albeit used in <cfscript> expressions only:

Operator Description Examples returning true1
Equal (==) If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers; if either operand is a string, the other one is converted to a string

3 == var1
"3" == var1
3 == '3'

Not equal (!=) Returns true if the operands are not equal. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison.

var1 != 4 var1 != "5"

Greater than (>) Returns true if the left operand is greater than the right operand.

var2 > var1

Greater than or equal (>=) Returns true if the left operand is greater than or equal to the right operand.

var2 >= var1 var1 >= 3

Less than (<) Returns true if the left operand is less than the right operand.

var1 < var2

Less than or equal to (<=) Returns true if the left operand is less than or equal to the right operand.

var1 <= var2
var2 <= 5

1 These examples assume that var1 has been assigned the value 3 and var2 has been assigned the value 4.

Below are just a few simple examples of the new ColdFusion operators and their use:

IS EQUAL TO

In the below example, the value of var1 is not equal to var2, therefore the comparison returns false and the else statement is processed, writing the number 4 to the screen.

<cfscript>
var1 = 3;
var2 = 4;
if (var1 == var2) //returns false
	writeOutput(var1);
else
	writeOutput(var2); //writes out 4
</cfscript>

IS NOT EQUAL TO

In the below example, the variable var1 is not equal to 4, therefore the statement returns true, writing 3 to the screen. The second if statement serves to emphasise that ColdFusion is not strictly typed. Therefore comparing var1 to an Integer or a String is possible. ColdFusion handles type conversion in the background.

<cfscript>
var1 = 3;
var2 = 4;
if (var1 != 4) //returns true
	writeOutput(var1); //writes out 3
 
if (var1 != "5") //returns true
	writeOutput(var1); //writes out 3
</cfscript>

IS GREATER THAN

In the below example, var2 is greater than var1, therefore the value of var2, 4, is printed out on screen. Changing the operator to Greater Than or Equal To (>=) would also return the same result. However, using the Less Than (<) and Less Than or Equal To (<=) operators would return false and not write a value to the screen.

<cfscript>
var1 = 3;
var2 = 4;
if (var2 > var1) //returns true
	writeOutput(var2); //writes out 4
</cfscript>

Following on from my posts on Arithmetic and Assignment operators, Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

The logical operators are described in the following table:

Operator Usage Description
Logical AND (&&) expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
Logical OR (||) expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.
Logical NOT (!) !expr Returns false if its single operand can be converted to true; otherwise, returns true.

Using ColdFusion, the Logical operators can be expressed as follows:

Logical AND

In the below example, both operands, x and y, return true, therefore myVariable is set to true within the logical statement.

<cfscript>
x = 9;
y = 2;
myVariable = false;
 
if (x < 10 && y > 1)
{
	myVariable = true;
}
</cfscript>

Logical AND Operator Example Results

Logical OR

In the below example, neither x equals 10 nor y equals 1, therefore the first part of the logical statement is not resolved. Instead, the else statement is processed, assigning the boolean value false to the variable myVariable.

<cfscript>
x = 9;
y = 2;
 
if (x == 10 || y == 1)
{
	myVariable = true;
}
else
{
	myVariable = false;
}
</cfscript>

Logical OR Operator Example Results

Logical NOT

In the below example, x does not equal y. Ordinarily this would result in the logical statement being evaluated, however, a logical negation has been applied to the beginning of the expression to be evaluated, therefore the variable myVariable is assigned the boolean value true.

<cfscript>
x = 9;
y = 2;
myVariable = false;
 
if (!x==y)
{
	myVariable = true;
}
</cfscript>

Logical NOT Operator Example Results

In my first post in this series, I introduced Using JavaScript Arithmetic Operators in ColdFusion 8. Now we’ll concentrate on Assignment operators.

An assignment operator assigns a value to its left operand based on the value of its right operand.

The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x. The other assignment operators are usually shorthand for standard operations, as shown in the following table.

Operator Shorthand operator Meaning
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y

In unusual situations, the assignment operator is not identical to the Meaning expression in the above table. When the left operand of an assignment operator itself contains an assignment operator, the left operand is evaluated only once. For example:

<cfscript>
a[i++] += 5 //i is evaluated only once
a[i++] = a[i++] + 5 //i is evaluated twice
</cfscript>

An often mentioned complaint by ColdFusion developers is the lack of operators commonly found in other programming languages such as JavaScript. For example, instead of the greater-than (>) symbol we have been restricted to the more wordy GT or GREATER THAN operator. However, in ColdFusion 8 this has changed and we have more freedom to use familiar JavaScript operators in <cfscript> blocks.

In the following series of posts, I will introduce the changes and show some simple examples. The first in the series is Arithmetic Operators.

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). These haven’t changed from ColdFusion version 7 to 8.

A key change comes with the use of Unary Operators. The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

These operators work as they do in most other programming languages.

% (Modulus)

The modulus operator is used as follows:

var1 % var2

The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2.

In ColdFusion this is written:

<cfif myQuery.currentRow % 2></cfif>

A good use of the modulus operator would be to alternate row colours in tabular data. The above code could be wrapped around a CSS class, dynamically changing the name of the class on each row iteration.

++ (Increment)

The increment operator is used as follows:

var++ or ++var

This operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.

For example, if x is 10, then the statement y = x++ sets y to 10 and increments x to 11. If x is 10, then the statement y = ++x increments x to 11 and sets y to 11.

In ColdFusion, a simple example:

<cfset x = 10 />
<cfset y = x++ />
 
<cfdump var="#variables#" label="Increment Operator Test 1" />

The above code returns:

Increment Operator Test 1

Switching the order of the operator to be before the variable:

<cfset x = 10 />
<cfset y = ++x />
 
<cfdump var="#variables#" label="Increment Operator Test 2" />

The above code returns; note that both variables return 11:

Increment Operator Test 2

-- (Decrement)

The decrement operator is used as follows:

var-- or --var

This operator decrements (subtracts one from) its operand and returns a value. If used postfix (for example, x–), then it returns the value before decrementing. If used prefix (for example, –x), then it returns the value after decrementing.

For example, if x is three, then the statement y = x– sets y to 3 and decrements x to 2. If x is 3, then the statement y = –x decrements x to 2 and sets y to 2.

In ColdFusion, a slightly more complex example:

<cfscript>
//decrement the variable y
for(y=5; y > 0; y--)
{
	writeOutput(y);
}
//returns 54321
</cfscript>

The above code loops from 5 to 1, decrementing the value of y by 1 on each iteration. The returned output is therefore 54321.

We could now combine the increment and decrement operators to do even more complex operations. E.g.:

<cfscript>
//create a new array
arrTemp = arrayNew(1);
//create a new variable for the array counter
x = 1;
//loop y, decrement the variable y
for(y=5; y > 0; y--)
{
	arrTemp[x++] = y;
}
</cfscript>
 
<cfdump var="#arrTemp#" label="Combined Increment Decrement Operator Test" />

The above code loops from 5 to 1, decrementing the value of y by 1 on each iteration. At the same time, the value of x increments by 1 on each iteration and is used to define the array index, i.e. arrTemp[1].

The returned output can be displayed as follows:

Combined Increment Decrement Operator Test

Such code serves to demonstrate how you can set up complex operations without the need to use the following, antiquated operations:

<cfset x = incrementValue(x) />
<cfset y = y+1 />
<cfset a = decrementValue(a) />
<cfset b = b-1 />

- (Unary Negation)

The unary negation operator precedes its operand and negates it. For example, y = -x negates the value of x and assigns that to y; that is, if x were 3, y would get the value -3 and x would retain the value 3. This is not new to ColdFusion 8, but it is worth a mention.

In ColdFusion, this can be shown by the following:

<cfset x = 3 />
<cfset y = -x />
 
<cfdump var="#variables#" label="Unary Operator Test" />

The above code returns:

Unary Operator Test

Silverlight aims to compete with Adobe Flash and the presentation components of AJAX. It also competes with Sun Microsystems’ JavaFX, which was launched a few days after Silverlight.

Microsoft Silverlight is a proprietary runtime for browser-based Rich Internet Applications, providing a subset of the animation, vector graphics, and video playback capabilities of Windows Presentation Foundation. The runtime is available for Microsoft Windows and Mac OS X, with Linux support under development via the third-party Moonlight runtime.

Microsoft describes its advantages as follows:

Compelling Cross-Platform User Experiences

  • Deliver media experiences and rich interactive applications for the Web that incorporate video, animation, interactivity, and stunning user interfaces.
  • Seamless, fast installation for users, thanks to a small, on-demand, easy-to-install plug-in that is under 2 megabytes (MB) in size and works with all leading browsers.
  • Consistent experiences between Windows-based and Macintosh computers without any additional installation requirements.
  • Create richer, more compelling Web experiences that take greater advantage of the client for increased performance.
  • Stunning vector-based graphics, media, text, animation, and overlays that enable seamless integration of graphics and effects into any existing Web application.
  • Enhance existing standards/AJAX-based applications with richer graphics and media, and improve their performance and capabilities by using Silverlight.

Flexible Programming Model with Collaboration Tools

  • Based on the Microsoft .NET Framework, Silverlight enables developers and designers to easily use existing skills and tools to deliver media experiences and rich interactive applications for the Web.
  • Simple integration with existing Web technologies and assets means Silverlight works with any back-end Web environment or technology. No “rip and replace” required.
  • Silverlight integrates with your existing infrastructure and applications, including Apache, PHP, as well as JavaScript and XHTML on the client.
  • Choice of development languages including JavaScript, Ruby, Python, C#, Visual Basic .NET, and more.
  • Role-specific tools for both designers and developers that take advantage of Web standards and the breadth of the Microsoft .NET connected software features.
  • For designers: Microsoft Expression Studio for creating interactive user interfaces and media rich experiences, preparing media for encoding and distribution, and creating World Wide Web Consortium (W3C) standards-compliant sites using modern XHTML, XML, XSLT, CSS, and ASP.NET.
  • For developers: Microsoft Visual Studio for developing client and server code with full Microsoft IntelliSense, powerful cross-platform debugging, rich language support, and more.
  • Consistent presentation model by using XAML, the declarative presentation language used in Windows Vista–based applications. Controls, visual designs, media, and other elements can be presented with full design fidelity in both Silverlight and Windows-based applications.
  • Extensible control model makes it easy to add rich content and behaviors while enabling efficient code-reuse and sharing.
  • Dramatically improved performance for AJAX-enabled Web sites with the power, performance, and flexibility of Silverlight and .NET-connected software.

High Quality, Low Cost Media

  • Unified media format that scales from high definition (HD) to mobile with Windows Media Video (WMV), the Microsoft implementation of the Society of Motion Picture and Television Engineers (SMPTE) VC-1 video standard, as well as support for Windows Media Audio (WMA) and MP3 audio.
  • Add vector-based graphics and overlays to media with support for integration of graphics that scale to any size and broadcast-style overlays for tickers and closed captioning.
  • Flexible ad-insertion solutions with video and animation, including the ability to deliver fluid, broadcast-style video or animated advertisements without loss of visual fidelity or motion quality.
  • Lower-cost media streaming with Emmy Award winning Windows Media technologies that can lower the cost of streaming delivery by up to 46%, and enjoy the flexibility to work with your existing Windows Media streaming deployments. Even further cost reductions are possible with the upcoming Microsoft Internet Information Services (IIS) Media Pack for Microsoft Windows Server 2008.
  • Broad ecosystem of media tools, servers, and solutions compatible with the Windows Media operating system.
  • Microsoft PlayReady content-access technology that delivers a single solution for digital rights management support on both Windows-based and Macintosh computers for content providers (coming in Silverlight 1.1)
  • Powerful encoding tools for live and on-demand publishing of media experiences with Microsoft Expression Encoder, including hardware-accelerated encoding of WMV and VC-1 at up to 15 times the performance of software alone when paired with a Tarari Encoder Accelerator board.

Connected to Data, Servers, and Services

  • Mash-up and incorporate services and data from the Web by taking advantage of the Silverlight support for LINQ while accessing that data with common protocols like JSON, RSS, POX, and REST.
  • Increase discoverability of rich interactive application (RIA) content that can be indexed and searched due to the text-based XAML format that describes interface and content in a Silverlight-based application.
  • Rapidly scale applications with Silverlight Streaming by Windows Live to host and integrate software services and media content.

Streaming audio and video

  • Silverlight Streaming by Windows Live offers a free streaming and application hosting solution for delivering high-quality, cross-platform, cross-browser, media-enabled rich interactive applications (RIAs). With the ability to author content in Microsoft Expression Encoder and other third-party editing environments, Web designers maintain complete control of the user experience.

Douglas Crockford, in a four part series on Yahoo! Video, gives a great overview of the Javascript programming language and clears ups some misconceptions along the way.

Part 1:

Part 2:

Part 3:

Part 4:

Slides for the presentation can be downloaded from:

http://yuiblog.com/assets/crockford/javascript.zip

The Internet has emerged from obscurity to become a dominant platform for application development and is integral to the idea of Software as a Service (SaaS). Unfortunately the demand to build applications of increasing complexity has continued to outpace the ability of traditional Web applications to represent that complexity and expectation. Utilisation of AJAX technologies attempts to reconcile some of the issues, but frequently the result is a frustrating, confusing or disengaging user experience resulting in unhappy customers, lost sales, and increased costs.

We are in a period of expanding opportunity for Internet and intranet applications. The growth in adoption and usage of the Internet has acted as a driver behind technology spending, spawned such terms as Service Orientated Architecture (SOA), Software as a Service (SaaS) and Web Services, and enterprise integration trends that seek to combine back-office infrastructures with new front-office applications and the Internet.

Integral to this is the need to communicate better with employees, customers, suppliers, and partners. Intranet applications, including enterprise information portals and employee facing applications, are increasingly depended upon to share information across a company, while outwardly focused extranet applications seek to more tightly bind networks of partners, suppliers and customers and make communication, business transactions and support easier.

A key reason Web applications cannot represent these types of complexity is because of the limitations of HTML pages. The Internet grew up on the notion of a network of loosely coupled, unintelligent clients that communicate with increasingly intelligent servers by sending requests for pages. The emergence of Rich Internet Applications (RIA’s) has served to blur the distinction between the desktop and the Web and has resulted in smart, powerful and dynamic user interfaces. RIA’s seek to combine the best of the desktop, Web and communication technologies.

As one would expect, the driving forces behind Rich Internet Applications are the big guns in the technology and Web industry; namely Adobe, Google and Microsoft. Each company has produced their own RIA platforms:

Rich Internet Applications

Adobe Integrated Runtime (AIR)

AIR is a cross-operating system runtime that allows developers to leverage their existing web development skills Flash, Flex, HTML, Ajax) to build and deploy desktop RIA’s.

Applications can be built using the following technologies:

  • Flash / Flex / ActionScript
  • HTML / JavaScript / CSS / AJAX
  • Combination of these technologies
  • PDF can be leveraged with any application

Adobe Integrated Runtime can be found at http://labs.adobe.com/technologies/air/

Google Gears

Google Gears is an open source browser extension that lets developers create web applications that can run offline.

Google Gears consists of three modules that address the core challenges in making web applications work offline.

  • LocalServer Cache and serve application resources (HTML, JavaScript, images, etc.) locally
  • Database Store data locally in a fully-searchable relational database
  • WorkerPool Make your web applications more responsive by performing resource-intensive operations asynchronously

Google Gears can be found at http://gears.google.com

Micrsoft Silverlight

Silverlight is a cross-browser, cross-platform plug-in for delivering the next generation of .NET based media experiences and rich interactive applications for the Web. Silverlight offers a flexible programming model that supports AJAX, VB, C#, Python, and Ruby, and integrates with existing Web applications. Silverlight supports fast, cost-effective delivery of high-quality video to all major browsers running on the Mac OS or Windows.

Microsoft Silverlight can be found at http://silverlight.net

What is AJAX?

Asynchronous JavaScript and XML. It’s a whole new way of looking at the web where HTML page makes asynchronous calls to the server using JavaScript and loads the data in bits and pieces as needed. Ajax is not a new technology. It’s a new developing approach, based on the following existing technologies:

  • XHTML and CSS for standard presentation,
  • DOM (Document Object Model) for dynamic and interactive presentation,
  • XML and XSLT for data exchange and manipulation, and
  • XMLHttpRequest for asynchronous data retrieval

The term “AJAX” was first muted by Jesse James Garrett of AdaptivePath and has become synonymous with the ideas and concepts of Web 2.0. Ajax has been popularised by the likes of Google in their Gmail and Google Suggest applications, Flickr and del.icio.us, now both owned by Yahoo!.

Below I have listed a few of the frameworks available to the ColdFusion community. I will leave it up to you to decide which one is the best and easiest to implement. Please tell me your experiences.

AjaxCFC

AjaxCFC, created by Rob Gonda, is a ColdFusion framework meant to speed up Ajax application development and deployment by providing developers seamless integration between JavaScript and ColdFusion, and providing built-in functions, such as security and debugging, to quickly adapt to any type of environment and helping to overcome cross-browser compatibility problems.

  • ColdFusion components following the best practices of object oriented programming and design patterns. Programming with ajaxCFC involves extending components and creating your own ajax façades.
  • Intergration with Model-Glue, one of the most popular MVC frameworks widely used by the ColdFusion community.
  • Works with ColdFusion MX 6.0, 6,1, 7.0 and Blue Dragon.
  • Automatically handles complex object transmitted from the client to the server and vice versa.
  • Server returns pure JavaScript code to the callback handler (instead of XML or JSON) to vastly improve performance.
  • On-the-works: Built-in base64 and/or blowfish encryption.
  • Licensed under the Apache License Version 2.0, by Rob Gonda.

The project can be downloaded from Rob Gonda’s website at the following address: http://www.robgonda.com/blog/projects/ajaxcfc/

JSMX

JSMX is a simple API available for connecting your Web Applications to an AJAX front end. The main difference between JSMX and other AJAX implementations is that JSMX allows you to pass either XML or JavaScript to the API. JSMX was originally created to be used with ColdFusion applications because of how easy it is to create JavaScript Strings natively within ColdFusion (using either the CFWDDX tag or the ToScript() function). However, because there is no server-side component to be installed, JSMX can really be used with any programming language.

  • Using the CFWDDX tag, or the toScript() function, within ColdFusion makes converting your ColdFusion Objects to JavaScript a SNAP!
  • Smaller Packet Sizes over the wire (JavaScript Vs. XML).
  • Reduced latency due to less parsing of the responses.
  • Parameters can be sent to the server in multiple formats including, strings, objects, and entire forms without having to build extra logic to handle each type.
  • API has no Server Side components which makes it more portable.
  • Extremely simple syntax shortens the learning curve and speeds up development.
  • Open-source (Creative Commons Attribution-ShareAlike License).

JSMX is the creation of Todd Kingham at LaLaBird.com and can be downloaded from the following link: http://www.lalabird.com/?fa=JSMX.downloads

CFAjax

CFAjax is the AJAX implementation for coldfusion. It makes ColdFusion method calls on server directly from HTML page using JavaScript and return backs the result to the calling HTML page. CFAjax comes with simple to use JavaScript API and simple ColdFusion implementation that marshal’s the response between your ColdFusion methods and HTML page. Using CFAjax you can create highly interactive websites with greater performance and usability.

CFAjax can be downloaded at the following link: http://www.indiankey.com/cfajax/project.asp

SAJAX for ColdFusion

Sajax is an open source tool to make programming websites using the Ajax framework — also known as XMLHTTPRequest or remote scripting — as easy as possible. Sajax makes it easy to call PHP, Perl or Python functions from your webpages via JavaScript without performing a browser refresh. The toolkit does 99% of the work for you so you have no excuse to not use it.

Sajax for ColdFusion is the creation of Steve Smith at Ordered List and can be downloaded from the following link: http://www.orderedlist.com/downloads/SAJAX_ColdFusion.zip

Newer entries »