Home / Articles / Learn to Write Script-Based Syntax in ColdFusion 8: It's Easier Than It Sounds

Learn to Write Script-Based Syntax in ColdFusion 8: It's Easier Than It Sounds

Chapter Description

The world's leading ColdFusion experts show you how to use script-based syntax to make writing code just a little easer in ColdFusion 8.

Common Problems and Solutions

Even with the removal of many incompatibilities in script-based operators (as compared to JavaScript and other languages), there are some challenges as ones switches between using tag- and script-based coding. To resolve errors in your scripts, follow these simple guidelines:

  • Make sure each statement ends with a semicolon (;).
  • For every opening ( and {, make sure there is a corresponding closing ) and }.
  • Check for closing tag > symbols when converting tag-based code to script. It’s easy to forget to remove the > symbol at the end of each tag, which you need to replace with a semicolon (;).
  • Make sure your statements don’t contain any ColdFusion tags, because <cfscript> doesn’t allow these.

A Note About the {} Symbols

As you’ve seen in the script examples so far, <cfscript> uses {} to create blocks of script code that the if or else part of an if-else statement should execute. Actually, the use of {} is optional if either the if or else keyword is handling only one line of code. If you leave the brackets out, however, things can get confusing should you need to add additional lines to your code later. For that reason, including these symbols in your script code is a good habit, even when they aren’t explicitly needed.

The following code is an example of when you need to use {}. By itself, this code throws an error:

   <cfscript>
     a = 1;
     if(a == 1)
      b = 1;
      b = 2;
     else
      b = 3;
   </cfscript>

   <cfoutput>#b#</cfoutput>

By definition, this code should set the value of 2 to the variable b, but it doesn’t do that because the second expression, b = 2, isn’t evaluated. To make this code behave correctly, we must insert {} around clauses that contain two or more expressions:

   <cfscript>
     a = 1;
     if(a == 1) {
      b = 1;
      b = 2;
     } else {
      b = 3;
     }
   </cfscript>

   <cfoutput>#b#</cfoutput>

Now the code works as it should and returns 2 as the value held in the variable b.

A Note About Quotes

As in JavaScript, strings in <cfscript> containing single or double quotes (‘ or “) are a source of confusion and error. Any string surrounded with double quotes cannot contain unescaped double quotes within the string. In other words, if this string contains a quoted word or phrase, we have to escape each instance of double quotes with another instance of double quotes. To see a string that causes an error, follow the next example:

   <cfscript>
     MyVar = "Take it to the "next" level...";
   </cfscript>

Because the quotes delimit a string, having quotes of the same type within the string causes the interpreter to end prematurely. You can work around this issue by alternating between double and single quotes within a string. If double quotes surround your string, use single quotes within it:

   <cfscript>
     MyVar = "Take it to the 'next' level...";
   </cfscript>

If you need to maintain double quotes within your string, simply insert a second set to escape them:

   <cfscript>
     MyVar = "Take it to the ""next"" level...";
   </cfscript>
4. User-Defined Functions in <cfscript> | Next Section Previous Section

There are currently no related articles. Please check back later.