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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accssing JavaScript Variable in ColdFusion

Status
Not open for further replies.

Kush

Programmer
Sep 20, 2000
24
0
0
US
How can I access a JavaScript variable in ColdFusion?
For example, if I set a variable var1 in JavaScript as follows,
<script language=&quot;javascript&quot;>
function f(temp)
{
var var1;
if temp == &quot;Yes&quot;
{
var1 = &quot;Yes&quot;;
}
else if YesNo == &quot;No&quot;
{
var1 = &quot;No&quot;;
}
}
</script>

In my ColdFusion code, how can I access the value of var1.
I am not able to use <cfset temp = var1> directly.

Thanks in anticipation.


 
try this:


<cfset cfvar_1 = &quot;<script>document.write(var1)</script>&quot;>

<cfoutput>cfvar_1: #cfvar1#</cfoutput> Sylvano
dsylvano@hotmail.com
 
The problem with trying to do something like that is, by the time you get to the JavaScript portion of your page, the CF has already been processed on the server. JS is client-side while CF is server-side.

You have a couple of options, depending on what you want to do though.
Code:
1.  You can do things the other way around...use a CF
    variable in your JS, but it doesn't sound like that's
    what you're looking for.

2.  You can create your JS variable then put it in a form
    field (hidden field if you'd like) then pass it to a 
    second page which contains your CF and call it using
    the FORM.myVariable syntax.

3.  You can do the same as above, but post the form to the
    same page you're on, in which case you'd need to make
    sure you first declare the variable as a CFPARAM or
    something so CF doesn't try to use it before it is
    created.
Stuff like this can be done, but with one language being server-side and the other being client-side you have to do some massaging in order to make it work.

Hope this helps. Kevin
slanek@ssd.fsi.com
 
hi Kush

WWebSpider's idea won't work. What exactly are you trying to do? Javascript is the last thing the .CFM file will be scanned for, CF tags being closer to the first.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top