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.

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

This question was written by Jeremy Petersen
It was last updated on March 23, 2006.

Categories

JavaScript

Comments

comments powered by Disqus