Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coldfusion vars within JavaScript

Status
Not open for further replies.

johne417

Programmer
Joined
Sep 18, 2001
Messages
23
Location
US
Is there a way to get a JavaScript variable to be put into a Coldfusion Variable? To illustrate my issue in a very simple example:

<cfparam name=&quot;userID&quot; default=&quot;&quot;>

<SCRIPT language=&quot;JavaScript&quot;>
<!--
<cfoutput>
var temp = 1;
[somehow do something like &quot;#userid# = temp&quot;]
<cfoutput>
-->
</SCRIPT>

<cfoutput>userid: #userid#</cfoutput>

And I'm of course trying to get the #userid# output to be 1 in this example. I played around with eval() and a couple other things and couldn't get it. Any help is greatly appreciated.
 
See the first FAQ in the FAQ area:
faq232-171

DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Well... I've read your question a couple of times, and I'm still confused.

Your subject says &quot;Coldfusion vars within JavaScript&quot;... yet some of your question seems to say you want to use javascript vars within ColdFusion... and some seems to imply you want the other way around.

It's important to note that ColdFusion is processed on the server-side. As such, all processing is completed before the browser/client ever sees it. Javascript, by contrast, is processed client-side.

By the time Javascript processes, ColdFusion is entirely done with it's side of things. So you can not set ColdFusion variables from within a javascript function, no.

You can easily use ColdFusion variables in javascript, however, for exactly the same reason.

Look at:
Code:
<CFSET myVar = &quot;Hello world&quot;>

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
   function GetValue(){
       <CFOUTPUT>return &quot;#myVar#&quot;</CFOUTPUT>;
   }

   alert (GetValue());
</script>

since ColdFusion has already processed all the CFML before sending the page to the browser, what the browser processes turns out to be:
Code:
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
   function GetValue(){
       return &quot;Hello world&quot;;
   }

   alert (GetValue());
</script>

so you should get an alert box that displays what was, at one time, a the ColdFusion variable myVar.


You can also use the ColdFusion function CFWDDX as a handy way to utilize ColdFusion's more complex data types (like array and structure) in javascript:
Code:
<cfwddx action=&quot;CFML2JS&quot; ...>


But there is no way to go the other way. You can't, for instance, say:
Code:
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
   function SetValue(inStr){
       <CFSET myVar = inStr>
   }
</script>

one, because &quot;inStr&quot; would not exist as a ColdFusion variable (unless you declared it)... so just-in-time error handling would throw an error right there. But also, even if inStr existed in ColdFusion space, what the browser would receive to process would be:
Code:
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
   function SetValue(inStr){
   }
</script>

so, in effect, nothing would happen in Javascript space (except maybe an error due to an empty function... can't remember if that's illegal or not).


The only way to get javascript variables into ColdFusion is to make another roundtrip to the server. This is usually done through the use of a form submittal:
Code:
[code]
<CFPARAM name=&quot;FORM.myVar&quot; default=&quot;Hello World&quot;>

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
   var myJSVar = &quot;Goodbye&quot;;
   function GetValue(){
       <CFOUTPUT>return &quot;#FORM.myVar#&quot;</CFOUTPUT>;
   }

   alert (GetValue());
   document.myForm.myVar.value = myJSVar ;
</script>

<CFOUTPUT><form action=&quot;#GetFileFromPath(GetBaseTemplatePath())#&quot; name=&quot;myForm&quot; method=&quot;POST&quot;></CFOUTPUT>
   <input type=&quot;hidden&quot; name=&quot;myVar&quot;>
   <input type=&quot;submit&quot; name=&quot;submit&quot;>
</form>

This is pretty silly code (ie - practically useless, and you'd never do this), but it illustrates the technique. The first time the page is loaded, an alert dialog would appear with the text &quot;Hello World&quot;. Then, through javascript, you set the value of a hidden form element to a javascript variable. When you submit the form, that javascript value is submitted back to ColdFusion to process (write as text in the GetValue() function)... so that the alert dialog now says &quot;Goodbye&quot;. (pretty sure it works... it's off the top of my head and untested)

The other thing you can do is drop a Cookie in javascript with a given value. But it would still require another roundtrip to the server for ColdFusion to then read that Cookie and make use of the value. But, that way, you could do it by means of something other than a form submittal (like a location.href or some sort of timer that refreshes the page).




-Carl
 
Sorry... that last code block should be:

Code:
<CFPARAM name=&quot;FORM.myVar&quot; default=&quot;Hello World&quot;>

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
   var myJSVar = &quot;Goodbye&quot;;
   function GetValue(){
   	<CFOUTPUT>return &quot;#FORM.myVar#&quot;</CFOUTPUT>;
   }
   alert (GetValue());
</script>

<CFOUTPUT><form name=&quot;myForm&quot; action=&quot;#GetFileFromPath(GetBaseTemplatePath())#&quot; method=&quot;POST&quot;></CFOUTPUT>
   <input type=&quot;hidden&quot; name=&quot;myVar&quot; value=&quot;&quot;>
   <input type=&quot;submit&quot; name=&quot;submit&quot;>
</form>

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
   document.myForm.myVar.value = myJSVar ;
</script>

before, it was attempting to set the value of the form field before the form or the form field existed, resulting in a javascript error. A whole 'nother joy of client-side scripting ;-)




-Carl
 
I was aware that JavaScript could see (sortof) Coldfusion variables and code, I was basically asking (looking back, in a rather roundabout way) if you could set/change Coldfusion variables within JavaScript. And my question's been answered, it's a no. Bummer, but I appreciate the help.
 
Not to discount your need... but there are usually better ways to accomplish what you're trying to do (9 times out of 10 anyway)... if you step back from the problem just a little bit.

I know your original code was just an example... but if you look at it:
Code:
<cfparam name=&quot;userID&quot; default=&quot;&quot;>

<SCRIPT language=&quot;JavaScript&quot;>
<!--
  <cfoutput>
    var temp = 1;
    [somehow do something like &quot;#userid# = temp&quot;]
  <cfoutput>
-->
</SCRIPT>

<cfoutput>userid: #userid#</cfoutput>

there isn't a whole lot that you could compute within the javascript block that you couldn't do in ColdFusion itself. I mean... if you know you want userid to be 1... set it to one in ColdFusion:
Code:
<cfparam name=&quot;userID&quot; default=&quot;1&quot;>

<SCRIPT language=&quot;JavaScript&quot;>
<!--
  <cfoutput>
    var temp = #userid#;
        :
  <cfoutput>
-->
</SCRIPT>

<cfoutput>userid: #userid#</cfoutput>

In fact... just about the only thing that you can't compute reliably server-side is the user's local time. But there are ways around that.

What, exactly, are you trying to do?



-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top