Common Problems and Solutions


If you're a JavaScript user, a few things about <cfscript> might not be apparent when you begin coding your scripts. Incorrect operators, for example, tend to be the most common error-causing elements for first-time <cfscript> users. As mentioned earlier, <cfscript> does not support operators such as >=, ==, ++, and many others seen in JavaScript. Instead, <cfscript> uses its own set of operators, such as gte, eq, and i=i+1.

So, in a simple for loop, JavaScript syntax would look like the following:

 <script Language="JavaScript">  for(i=0; i<=10; i++){  document.write(i);  } </script> 

In the conditional statement, JavaScript uses <= (less than or equal to) to test the condition. The looping variable i is then incremented with the i++ statement, which says "i equals the value of i plus 1." In <cfscript>, slightly different methods replace these, as seen in the following example:

 <cfscript>  for(i=0; i LTE 10; i=i+1){  WriteOutput(i);  } </cfscript> 

As you can see, we replace <= with LTE, which is ColdFusion's equivalent to that JavaScript operator. We also replace JavaScript's ++ increment operator with i=i+1, which is one way ColdFusion increments a value.

Another method of incrementing a value in ColdFusion is to use the IncrementValue() function:

 <cfscript>  for(i=0; i LTE 10; i=IncrementValue(i)){  WriteOutput(i);  } </cfscript> 

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 }.

  • Make sure every operator is correct. Use LT instead of <, EQ instead of ==, and i=i+1 instead of i++, for example.

  • 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 eq 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 eq 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> 

TIP

Unlike JavaScript, <cfscript> does not allow you to escape quotes with the backslash character (\).




Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net