User Guide
ToScript 889
Usage
To use a ColdFusion variable in JavaScript or ActionScript, the ToScript function must be in a
cfoutput region and be surrounded by number signs (#). For example, the following code uses
the
ToScript function to convert a ColdFusion variable to a JavaScript variable:
<cfset thisString="hello world">
<script type="text/javascript" language="JavaScript">
<cfoutput>
var #toScript(thisString, "jsVar")#;
</cfoutput>
</script>
When ColdFusion runs this code, it sends the following to the client:
<script type="text/javascript" language="JavaScript">
var jsVar = "hello world";
</script>
An HTML script tag must enclose the JavaScript code. The cfoutput tag does not need to be
inside the script block; it can also surround the block.
WDDX-style output generates JavaScript code that creates a WDDXRecordset object, where the
key of each record set entry is a column name, and the value of the recordlist entry is an array of
the corresponding query column entries, as follows:
WDDXQuery = new WddxRecordset();
col0 = new Array();
col0[0] = "John";
col0[1] = "John";
WDDXQuery["firstname"] = col0;
col0 = null;
col1 = new Array();
col1[0] = "Lund";
col1[1] = "Allen";
WDDXQuery["lastname"] = col1;
col1 = null;
To use WDDX-style output, you must first load the cf_webroot/CFIDE/scripts/wddx.js script,
which defines JavaScript WDDX objects, as in the following line:
<script type="text/javascript" src="/CFIDE/scripts/wddx.js script"> </script>
For more information on WDDX in JavaScript, see Chapter 9, “WDDX JavaScript Objects,” on
page 1091.
ActionScript-style output generates code that creates an array of objects, where the array is
indexed by row number, and the objects consist of column name - column value pairs, as follows:
ActionScriptQuery = new Array();
ActionScriptQuery[0] = new Object();
ActionScriptQuery[0]['firstname'] = "John";
ActionScriptQuery[0]['lastname'] = "Lund";
ActionScriptQuery[1] = new Object();
ActionScriptQuery[1]['firstname'] = "John";
ActionScriptQuery[1]['lastname'] = "Allen";