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!

Passing CF Variables to JavaScript

Status
Not open for further replies.

AAshcroft

Programmer
Joined
Oct 28, 2001
Messages
5
Location
CA
I'm a new CF user and can't figure out how to reference CF Variables from a JavaScript function. The CF Variables are set on the same page as the JavaScript function is coded. The example below is the simplified version of my problem - simple as it is, I can't seem to solve it.

I found the following method posted on the web, but it doesn't work for me:

<CFSET first_year_int = 1993>
<CFSET first_year_int = 1993>

<SCRIPT language=&quot;JavaScript&quot;>
function Calendar(month, year) {
var first_year_JSint = #first_year_CFint#;
var last_year_JSint = #last_year_CFint#;

for (year=first_year_JSint; year<(last_year_JSint + 1); year++){
.....output...
}

The output works fine when the variable names within the JS function are replaces with values. I've played with al sorts of syntax here, but can't get JavaScript to recognize my CF variables.

Any help would be appreciated!



 
I am not sure if this would work for your application but I have had success in the past using a form in my cf code and referring to that form and element name from javascript:

<SCRIPT language=&quot;JavaScript&quot;>
function Calendar(month, year) {
var first_year_JSint = document.formname.elementname.value;
var last_year_JSint = document.formname.elementname.value;

more script.....

</script>

<body>
<form name=formname action=... onsubmit=calendar>
any elements that the above javascript can reference..

<input type=hidden name elementname value=1993>
</form>

I hope this helps or gets you on the right track.
 
You have to enclose your variables with <cfoutput> , and it will work :)
 
try using the same name in both cases:

<CFSET first_year_int = 1993>
<CFSET first_year_int = 1993>

<SCRIPT language=&quot;JavaScript&quot;>
function Calendar(month, year) {
var first_year_JSint = <cfoutput>#first_year_int#</cfoutput>;
var last_year_JSint = <cfoutput>#last_year_int#</cfoutput>;
 
Thanks everyone, problem solved. Including the CF variables within <CFOUTPUT> tags (within the JavaScript code) is what is needed.

I played with cnisa's form method for amusement, but couldn't figure out how to get the results from my query (set using <CFSET> into the values of form elements.

The variable names were originally the same, reinr1. I was playing around with different methods, changing this and that. Obviously I was a bit sloppy when I posted my message. Thanks for pointing this out.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top