Whatever message this page gives is out now! Go check it out!
ToScript(cfvar, javascriptvar, outputformat, ASFormat) |
Parameter | Description |
cfvar | A ColdFusion variable. This can contain one of the following:
|
javascriptvar | A string that specifies the name of the JavaScript variable that the ToScript function creates. |
outputformat | Optional. A Boolean value that determines whether to create WDDX (JavaScript) or ActionScript style output for structures and queries:
|
ASFormat | Optional. A Boolean value that specifies whether to use ActionScript shortcuts in the script:
|
<cfset thisString="hello world">
<script type="text/javascript" language="JavaScript">
<cfoutput>
var #toScript(thisString, "jsVar")#;
</cfoutput>
</script><script type="text/javascript" language="JavaScript">
var jsVar = "hello world";
</script>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;<script type="text/javascript" src="/CFIDE/scripts/wddx.js"> </script> |
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";<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 --->
<cfquery name="thisQuery" datasource="cfdocexamples">
SELECT FirstName,LastName
FROM employee
WHERE FirstName = 'John'
</cfquery>
<br>
The Query in ColdFusion
<cftable query="thisQuery" headerlines="1" colheaders>
<cfcol align="left" width="9" header="<b>FirstName</b>" text="#FirstName#">
<cfcol align="left" width="9" header="<b>LastName</b>" text="#LastName#">
</cftable>
<strong>JavaScript generated by ToScript(thisQuery, "WDDXQuery"):</strong><br>
#toScript(thisQuery, "WDDXQuery")#;<br>
<br>
<strong>JavaScript generated by ToScript(thisQuery, "ActionScriptQuery",
False):</strong><br>
#toScript(thisQuery, "ActionScriptQuery", False)#<br>
<br>
<!--- Convert to both WDDX format and ActionScript format --->
<script type="text/javascript" language="JavaScript">
#ToScript(thisQuery, "WDDXQuery")#;
#ToScript(thisQuery, "ActionScriptQuery", False)#;
</script>
<!--- For brevity, this example does not use JavaScript query variables --->
</cfoutput>