Consider carefully what is happening here: The JavaScript code that is being executed client-side comes from the server, either as the result of a <script> tag in the delivered HTML, or as part of the HTML text itself. All of this material is (or can be) generated by the ColdFusion host, at the software designer's (i.e. your...) discretion. JavaScript text can also be delivered in response to an AJAX method-call (which, in a very real sense, is precisely what "JSON" is). When the source-code arrives at the client, the client has no idea where it came from, nor how it was produced. It's just "there," and it gets executed. So, to transfer a variable's value to the client, you need to send it to the client as a string (e.g. an assignment-statement) and you must ensure that the value is sufficiently encoded that you will always generate a syntactically-valid statement no matter what the value contains. (Quotes and so-forth.) A practical way to do this, if you know it could be a problem, is to use SerializeJSON() on the server-side and to subsequently decode it client-side. (In other words, the JavaScript source-code that you send consists of, say, var foo="json_encoded_string", and the client code must at some point decode it.) By all means, if you have a nice function like ToScript() on the server-side, and if you are satisfied with what it actually generates in all cases, then go ahead and use it. Don't make extra work for yourself whenever you can avoid it...
... View more