Well... I've read your question a couple of times, and I'm still confused.
Your subject says "Coldfusion vars within JavaScript"... 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 = "Hello world">
<script language="JavaScript" type="text/javascript">
function GetValue(){
<CFOUTPUT>return "#myVar#"</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="JavaScript" type="text/javascript">
function GetValue(){
return "Hello world";
}
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="CFML2JS" ...>
But there is no way to go the other way. You can't, for instance, say:
Code:
<script language="JavaScript" type="text/javascript">
function SetValue(inStr){
<CFSET myVar = inStr>
}
</script>
one, because "inStr" 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="JavaScript" type="text/javascript">
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="FORM.myVar" default="Hello World">
<script language="JavaScript" type="text/javascript">
var myJSVar = "Goodbye";
function GetValue(){
<CFOUTPUT>return "#FORM.myVar#"</CFOUTPUT>;
}
alert (GetValue());
document.myForm.myVar.value = myJSVar ;
</script>
<CFOUTPUT><form action="#GetFileFromPath(GetBaseTemplatePath())#" name="myForm" method="POST"></CFOUTPUT>
<input type="hidden" name="myVar">
<input type="submit" name="submit">
</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 "Hello World". 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 "Goodbye". (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