How can I convert ColdFusion variables into JavaScript variables?
Use the toScript() function to create JavaScript variables from a ColdFusion variable. This function can convert ColdFusion strings, numbers, arrays, structures, and queries to JavaScript syntax that defines equivalent variables and values.
<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>
This question was written by Jeremy Petersen.
It was last updated on March 23, 2006 at 3:56:18 PM EST.
CFML Referenced
Categories
Comments
Comment made by Mark Drew on March 23, 2006 at 11:26 AM
You can also do this using cfwddx to convert a coldfusion object into a javascript object which is handy for things like structs.
Comment made by Rob Brooks-Bilson on March 23, 2006 at 3:20 PM
Don't forget the new toScript() function in CFMX 7. It allows you to assign the value of a CF variable to a JavaScript (or ActionScript) variable. It does this for JavaScript through the magic of WDDX, but it's a lot simpler than using the WDDX libraries to get the same result.
Comment made by Jeremy Petersen on March 23, 2006 at 3:57 PM
Updated entry to use toScript() function.