Creates a JavaScript or ActionScript expression that
assigns the value of a ColdFusion variable to a JavaScript
or ActionScript variable. This function can convert
ColdFusion strings, numbers, arrays, structures, and
queries to JavaScript or ActionScript syntax that defines
equivalent variables and values.

toScript(cfvar, javascriptvar [, outputformat] [, asformat]) → returns string

Argument Reference

cfvar any

A ColdFusion variable. This can contain one of the following:
String, Number, Array, Structure or Query.

javascriptvar string

A string that specifies the name of the JavaScript variable
that the toScript function creates.

outputformat boolean
Default: true

A Boolean value that determines whether to create
WDDX (JavaScript) or ActionScript style output for
structures and queries.
Default: true
  • true
  • false

asformat boolean
Default: false

A Boolean value that specifies whether to use
ActionScript shortcuts in the script.
Default: false
  • true
  • false

Sample code invoking the toScript function

Converts coldfusion objects to JavaScript syntax for use


<h3>Converting a string variable</h3>
<cfset thisString = "This is a string">
<b>The thisString variable in ColdFusion</b><br>
<strong>The output of ToScript(thisString, "jsVar")</strong><br>
#ToScript(thisString, "jsVar")#<br>
<strong>In a JavaScript script, convert thisString Variable to JavaScript
and output the resulting variable:</strong><br>
<script type="text/javascript" language="JavaScript">
var #ToScript(thisString, "jsVar")#;
document.write("jsVar in JavaScript is: " + jsVar);

<h3>Converting an array</h3>
<!--- Create and populate a one-dimensional array --->
<cfset myArray=ArrayNew(1)>
<cfloop index="i" from="1" to="4">
<cfset myArray[i]="This is array element" & i>

<b>The ColdFusion myArray Array</b><br>
<!--- Write the contents of the myArray variable in ColdFusion --->
<cfloop index="i" from="1" to="#arrayLen(myArray)#">
myArry[#i#]: #myArray[i]#<br>
<strong>The output of ToScript(myArray, "jsArray")</strong><br>
#toScript(myArray, "jsArray")#<br>
<strong>In JavaScript, convert myArray to a JavaScript variable and write it's contents</strong><br>
<script type="text/javascript" language="JavaScript">
var #ToScript(myArray, "jsArray")#;
for (i in jsArray)
document.write("myArray[" + i + "]: " + jsArray[i] + "<br>");
<h3>Converting a query</h3>
This section converts the following query object to both WDDX format
and ActionScript type JavaScript objects.<br>

<!--- Query a database --->
<cfset news = queryNew("id,title", "integer,varchar")>
<cfset queryAddRow(news)>
<cfset querySetCell(news, "id", "1")>
<cfset querySetCell(news, "title", "Dewey defeats Truman")>
<cfset queryAddRow(news)>
<cfset querySetCell(news, "id", "2")>
<cfset querySetCell(news, "title", "Men walk on Moon")>
<cfset writeDump(news)>

<!--- run QofQ (query of query) --->
<cfquery name="sortedNews" dbtype="query">
    SELECT id, title FROM news
    ORDER BY title DESC
The Query in ColdFusion
<cftable query="sortedNews" headerlines="1" colheaders>
<cfcol align="left" width="9" header="<b>ID</b>" text="#id#">
<cfcol align="left" width="9" header="<b>title</b>" text="#title#">

<strong>JavaScript generated by ToScript(sortedNews, "WDDXQuery"):</strong><br>
#toScript(sortedNews, "WDDXQuery")#;<br>
<strong>JavaScript generated by ToScript(sortedNews, "ActionScriptQuery",
#toScript(sortedNews, "ActionScriptQuery", False)#<br>
<!--- Convert to both WDDX format and ActionScript format --->
<script type="text/javascript" language="JavaScript">
#ToScript(sortedNews, "WDDXQuery")#;
#ToScript(sortedNews, "ActionScriptQuery", False)#;
<!--- For brevity, this example does not use JavaScript query variables --->

Fork me on GitHub