Ask Ben: Converting Javascript Variables Into ColdFusion Variables

Posted June 22, 2009 at 9:51 AM

Tags: ColdFusion, Javascript / DHTML, Ask Ben

How can I convert a JavaScript variable into a coldfusion variable?

I have been asked this question enough times that I figured it was be time to turn it into an actual Ask Ben blog post for future reference. Often times, when front-end developers start getting into server-side scripting (hopefully using ColdFusion), there is some confusion as to where the various parts of the page rendering work flow are executing. And, because of this confusion, developers get stuck trying to access one part of the work flow from another.


 
 
 

 
ColdFusion Executes On The Server - Javascript Executes On The Client (Web Browser).  
 
 
 

To clear up the confusion, I think you just need to step back and understand the request-response processing work flow:

  1. The server gets a request to deliver some content (at the given URL).
  2. The server constructs the response (in our case, an HTML page containing Javascript).
  3. The server returns the HTML data to the client (Web Browser).
  4. The data streams from the Server to the Client.
  5. The browser renders the response (HTML page) for the user at which time any embedded Javascript code executes.

Once we hit step #4, the ColdFusion server is no longer involved. In step #5, when the client renders the HTML response, it doesn't even have to know where the HTML data came from. As such, when the Javascript executes in step #5, it has no knowledge of nor any access to the ColdFusion portion of the page response work flow - these two points of execution happen at different times on different machines.

Javascript can, however, communicate with the ColdFusion server by making AJAX-based page requests. These HTTP requests are viewed by the ColdFusion server as completely new page requests by the client and do not have access to temporary ColdFusion variables created during previous requests (unless those variables were explicitly cached on the server).

So, the moral of the story is that you can't really turn a Javascript variable into a ColdFusion variable because the two are completely unrelated. Once you understand the page request / response work flow, this becomes obvious. If you want Javascript to send values back to the ColdFusion server for further processing, you can only do so via AJAX requests. I hope that this clears up some confusion.

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Reader Comments

Jun 22, 2009 at 9:13 PM // reply »
1 Comments

You can always write to a hidden form field if you are processing a form, etc.


Jun 23, 2009 at 12:56 AM // reply »
34 Comments

Great Post

When you are calling your JavaScript you can always have the included JavaScript file end in a .cfm which would help in some cases... This will allow you to load different content based on variables that you pass... you can always pass variables through JavaScript like so...

<script type="text/javascript"
src="javascript.cfm?var1=bob&var2=frank">
</script>

This essential will pass the variables to coldfusion in which you can convert the variables into coldfusion variables using the url variable... I have used this method a few times but I don't recommend it for really robust applications.


Jun 23, 2009 at 1:37 AM // reply »
1 Comments

What about <cfwddx action="cfml2js" ... />?
Sure, we can't use ColdFusion vars on the client side, but we can serialize them and return to the browser along with the rest of the JavaScript code.


Jun 23, 2009 at 4:59 AM // reply »
1 Comments

json format is very helpfull to pass variables between javascript and coldfusion in ajax calls. There are 2 coldfusion functions since cf8 DeserializeJSON() and SerializeJSON().
And from you javascript code you can serialize your JS variables to a json string by using the json serializer( http://www.JSON.org/json2.js ).


Jun 23, 2009 at 6:06 PM // reply »
7,481 Comments

@Jody,

Definitely true - a great technique.

@Alexey,

Yeah - using ColdFusion variables to help render the Javascript that ultimately gets executed by the browser is definitely some we can do, and is the easier of the two directions (server to client vs. client to server).

@Patrick,

JSON rocks! 'nuff said.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 11, 2010 at 9:14 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Just need to add - we still did have to get the external service to clean up their WSDL and flatten it as per the article I posted before ... read »
Mar 11, 2010 at 9:00 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
I finally got to the bottom of my problem - the fact was that the WSDL was full of complex types that Axis didn't like - I should have been able to receive it as a struct but that just didn't work so ... read »
Mar 11, 2010 at 8:55 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
@Dmitry, No problem my man. You should be able to just parse the SOAP response into XML and be able to access all the nodes that way. The only thing you have to be careful of is the namespaces whic ... read »
Mar 11, 2010 at 8:49 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
cfhttp response is located in it's FileContent property ???? You can't imagine how many times I had to work with cfinvoke and it's complex responses and their weird classes deserialization because I ... read »
Mar 11, 2010 at 8:43 AM
Ask Ben: Building An AJAX, jQuery, And ColdFusion Powered Application
@Nathan, Not saying this is the correct way to do this, but I've handled this in many different ways depending on how "secure" the function needs to be. In a case like yours (users needs to be l ... read »
Mar 11, 2010 at 7:57 AM
FLEX On jQuery: Turning HTML Links Into Standard UI Elements
@Peter, Yeah, that's how I build to - we have AJAX and jQuery where is enhances a given page... but we don't require it to tie the entire application together (it's still very much a request-respon ... read »
Mar 11, 2010 at 5:38 AM
Delaying ColdFusion Session Persistence Until User Logs In
Hmmm, I didn't get any email notification here - I definitely selected all the checkboxes (they're still ticked). ... read »
Mar 11, 2010 at 5:05 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Seems like I'm missing something. If I take this code and put it in a .cfm file and try to run it I get this error: Detail Premature end of file. ErrNumber 0 ExceptionMessage Premature end of f ... read »