ToScript

Creates a JavaScript expression that assigns the input BoxLang value to a JavaScript variable.

This is useful for converting BoxLang values to JavaScript variables in a script. It can convert:

  • Booleans

  • DateTimes

  • Numbers

  • Arrays

  • Queries

  • Strings

  • Structures

Method Signature

ToScript(cfvar=[any], javascriptvar=[string])

Arguments

Argument
Type
Required
Description
Default

cfvar

any

true

The value to convert to a script line; String, DateTimes, Number, Array, Structure or Query

javascriptvar

string

true

The name of the JavaScript variable to assign the result to.

Examples

Multiple to Script Examples

Converts boxlang objects to JavaScript syntax for use

Run Example

<h2>ToScript</h2>

<h3>Converting a string variable</h3>
<bx:set thisString = "This is a string" >
<bx:output>
<b>The thisString variable in Boxlang</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>
</bx:output>

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

<bx:output>
<b>The Boxlang myArray Array</b><br>
<!--- Write the contents of the myArray variable in Boxlang --->
<bx:loop index="i" from="1" to="#arrayLen( myArray )#">
myArry[#i#]: #myArray[ i ]#<br>
</bx:loop>
<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 --->
<bx:set news = queryNew( "id,title", "integer,varchar" ) >
<bx:set queryAddRow( news ) >
<bx:set querySetCell( news, "id", "1" ) >
<bx:set querySetCell( news, "title", "Dewey defeats Truman" ) >
<bx:set queryAddRow( news ) >
<bx:set querySetCell( news, "id", "2" ) >
<bx:set querySetCell( news, "title", "Men walk on Moon" ) >
<bx:set writeDump( news ) >

<!--- run QofQ (query of query) --->
<bx:query name="sortedNews" dbtype="query">
    SELECT id, title FROM news
    ORDER BY title DESC
</bx:query>
<br>
The Query in Boxlang
<bx:dump var="sortedNews">

<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 --->
</bx:output>

Additional Examples

Run Example

// Struct
Struct = {};
Struct[ 1 ] = "boxlang";
jsVar = JSONSerialize( Struct );
resStruct = ToScript( struct, "jsVar" );
writeDump( resStruct );
// Array
Array = [];
Array[ 1 ] = "boxlang";
jsVar = JSONSerialize( Array );
resArr = ToScript( Array, "jsVar" );
writeDump( resArr );
// Query
Query = queryNew( "name,age", "varchar,numeric", {
	NAME : "Susi",
	AGE : 20
} );
resQry = ToScript( Query, "Query" );
writeDump( resQry );
// String
Str = "test";
resStr = ToScript( Str, "Str" );
writeDump( resStr );
// Number
number = 10;
resNum = ToScript( number, "number" );
writeDump( resNum );

RESULT: jsVar = {"1":"boxlang"}; jsVar = ["boxlang"]; Query = {"columns":["name","age"],"data":[["Susi",20]]}; Str = "test"; number = 10;

Last updated

Was this helpful?