toScript

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
Required

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

javascriptvar string
Required

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
Values:
  • true
  • false

asformat boolean
Default: false

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

Examples
Sample code invoking the toScript function

Converts coldfusion objects to JavaScript syntax for use

<h2>ToScript</h2>

<h3>Converting a string variable</h3>
<cfset thisString = "This is a string">
<cfoutput>
<b>The thisString variable in ColdFusion</b><br>
#thisString#<br>
<br>
<strong>The output of ToScript(thisString, "jsVar")</strong><br>
#ToScript(thisString, "jsVar")#<br>
<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);
</script>
</cfoutput>

<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>
</cfloop>

<cfoutput>
<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>
</cfloop>
<br>
<strong>The output of ToScript(myArray, "jsArray")</strong><br>
#toScript(myArray, "jsArray")#<br>
<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>");
}
</script>
<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
</cfquery>
<br>
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#">
</cftable>

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

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub