Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Can you include ColdFusion #whatever# variables in JavaScript

New Here ,
Dec 15, 2009 Dec 15, 2009

I need to refer to some variables I created using ColdFusion.  I created the variables to use in a <cfmail> tag.  That works great.  However, I need to use those same variables in a bit of JavaScript code I'm writing that will be placed in the body of the page.  Does anyone know if this is possible and how it looks?

Thanks.

615
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 15, 2009 Dec 15, 2009

BobDavis3000 wrote:

Does anyone know if this is possible and how it looks?

No, it is not possible and it looks like a void or something else impossible.

You need to think about what a variable is.  It is a name to some piece of data in a memory location on the computer.  Then you got to think about where ColdFusion runs and where JavaScript runs.  ColdFusion runs on a server, JavaScript runs on a Client that could be thousands of miles away.

Now then ask yourself, how would a name to some piece of data in memory on the server do anything on a completely different system thousands of miles away?

To do what you seem to be trying to do, you must write your application to comunicate the data back and forth between the server and the client.

It is pretty easy to write some CFML that creates a JS variable that will be sent to the client, so that when the client runs the JavaScript the data is stored in the client's memory and the desired name is associated with it.

<cfoutput>

<script type="text/javascript">

   var aJSvar = #aCFvar#;

</script>

</cfoutput>

It is possible to write JavaScript code that makes requests of the Server for data.  If this uses the javascript xmlHTTPrequest() object, this technique is commonly called AJAX.

And there are many other facets of this that go beyond what I have time to explain here.

But you must always remember the ColdFusion runs on the server and JavaScirpt runs on the client and never will the twain meet.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 15, 2009 Dec 15, 2009

Thanks for the info.  Fantastic info.  I appreciate your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 15, 2009 Dec 15, 2009
Can you include ColdFusion #whatever# variables in JavaScript

Yes. Use the toScript function. This is from the documentation:

To use a ColdFusion variable in JavaScript or ActionScript, the ToScript function must be in a cfoutput region and be surrounded by number signs (#). For example, the following code uses the ToScript function to convert a ColdFusion variable to a JavaScript variable:

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 15, 2009 Dec 15, 2009

BKBK wrote:

When ColdFusion runs this code, it sends the following to the client:

<script type="text/javascript" language="JavaScript">
    var jsVar = "hello world";
</script>

So would the simple:

<cfoutput>

<script type="text/javascript" language="JavaScript">

    var jsVar = '#thisString#';

</script>

</cfoutput>

But the toScript() function does make it much easier to output more complex variables like arrays or strucures.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 16, 2009 Dec 16, 2009
LATEST

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...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources