Writing Output

You can’t directly output text or variable values to the browser within a CFSCRIPT block. There is, however a function called WriteOutput( ) that you can use to write directly to the page output stream. This technique can output both variable values and plain text from within a CFSCRIPT block. Example 18-1 demonstrates the WriteOutput( ) function.

Example 18-1. Using the WriteOutput Function Within a CFSCRIPT Block

<H2>Using WriteOutput Within CFSCRIPT</H2>

<CFSCRIPT>
 x = 1;
 y = 2;
 MyMessage = "Hello World";
 
 WriteOutput("x = #x# <BR>");
 WriteOutput("MyMessage = #MyMessage# <BR>");
 WriteOutput(x+y); 
</CFSCRIPT>

In this case, because there is no way to embed HTML directly within the CFSCRIPT block, all formatting is done by embedding the HTML code within the WriteOutput( ) function. Executing Example 18-1 results in the output shown in Figure 18-1.

Using WriteOutput within a CFScript block to write to the page output stream

Figure 18-1. Using WriteOutput within a CFScript block to write to the page output stream

It is also possible to output the value of variables set within a CFSCRIPT block outside of the CFSCRIPT tags. This is accomplished by using a CFOUTPUT block outside the CFSCRIPT block to handle the actual output, as shown in Example 18-2.

Example 18-2. Outputting CFScript Variables Outside the CFSCRIPT Block

<H2>Using CFOUTPUT To Output Variables Created Within CFSCRIPT</H2> <CFSCRIPT> x = 1; y = 2; MyMessage = "Hello World"; </CFSCRIPT> ...

Get Programming ColdFusion now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.