Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks for the info. Fantastic info. I appreciate your help.
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Consider carefully what is happening here: