Adobe ColdFusion 8

cfscript

Description

Encloses a code block that contains cfscript statements.

Category

Application framework tags, Other tags

Syntax

<cfscript> 
    cfscript code here 
</cfscript>

Note: You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

See also

cfinvoke, cfmodule, CreateObject; "Extending ColdFusion Pages with CFML Scripting" in the ColdFusion Developer's Guide

History

ColdFusion MX:

  • Changed how to invoke component methods: this tag can now invoke component methods, using the CreateObject function
  • Changed use of reserved words: you cannot use ColdFusion reserved words within this tag
  • Added the try and catch statements.

Usage

Performs processing in CFScript. This tag uses ColdFusion functions, expressions, and operators. You can read and write ColdFusion variables within this tag.

For a detailed description of the CFScript scripting language, including documentation of CFScript statements and the CFScript equivalents of CFML tags, see "Extending ColdFusion Pages with CFML Scripting" in the ColdFusion Developer's Guide.

You can use this tag to enclose a series of assignment statements that would otherwise require cfset statements.

Important: If you code a cftry/cfcatch block within this tag using an exception's Java class name, you must provide the fully qualified class name.

You cannot use some ColdFusion reserved words in this tag. You cannot put a user-defined function whose name begins with any of these strings within this tag:

  • cf
  • cf_
  • _cf
  • coldfusion
  • coldfusion_
  • _coldfusion

You cannot use the elseif construct within a cfscript tag. You can use code such as the following:

else if ( condition )
{
...
}

Exception handling with the cfscript tag

To handle exceptions with this tag, use try and catch statements, which are equivalent to the cftry and cfcatch tags. For each try statement, you must have a catch statement. In the catch block, the variable exceptionVariable contains the exception type. This variable is the equivalent of the cfcatch tag built-in variable cfcatch.Type. For more information, see "Extending ColdFusion Pages with CFML Scripting" in the ColdFusion Developer's Guide.

Invoking ColdFusion components with the cfscript tag

CFScript invokes component methods using the CreateObject function.

The following example shows how to invoke a component object with the cfscript tag, using ordered arguments:

<cfscript>
quote = CreateObject( "component", "nasdaq.quote" ) ;
<!--- Invocation using ordered arguments. --->
res = quote.getLastTradePrice( "macr" ) ;
</cfscript>

The following example shows how to use an attribute collection within the cfscript tag to pass parameters when invoking a component object. An attribute collection is a structure in which each key corresponds to a parameter name and each value is the parameter value passed for the corresponding key.

<cfscript>
    stArgs = structNew();
    stArgs.zipcode = "55987";
</cfscript>
...
<cfinvoke
    webservice = "http://www.xmethods.net/sd/2001/TemperatureService.wsdl"
    method = "getTemp"
    argumentCollection = "#stArgs#"
    returnVariable = "aTemp"    > 
<cfoutput>The temperature at zip code 55987 is #aTemp#</cfoutput>    

In this example, the structure is created in a cfscript block, but you can use any ColdFusion method to create the structure.

Consuming web services with the cfscript tag

The following example shows how to consume a web service with the cfscript tag. You use the CreateObject function to connect to the web service.

<cfscript>
    ws = CreateObject("webservice", 
                    "http://www.xmethods.net/sd/2001/TemperatureService.wsdl");
    xlatstring = ws.getTemp("55987");
    writeoutput(xlatstring);
</cfscript>

For more information, see "Using Web Services" in the ColdFusion Developer's Guide.

Example

<p>This simple example shows variable declaration and manipulation.
<cfif IsDefined("form.myValue")>
    <cfif IsNumeric(form.myValue)>
        <cfset x = form.myValue>
        <cfscript>
            y = x;
            z = 2 * y;
            StringVar = form.myString;
        </cfscript>
    <cfoutput>        <p>twice #x# is #z#.
        <p>Your string value was: <b><I>#StringVar#</i></b>    </cfoutput>
<cfelse>