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

Accessing CF Variables in a JavaScript Function 1

Status
Not open for further replies.

Kush

Programmer
Sep 20, 2000
24
US
Can I access a ColdFusion variable in a JavaScript function?

For example:

<SCRIPT>
function f()
{
alert(&quot;Message is&quot; + m);
}

<CFSET m = &quot;Hello World&quot;>
<CFFORM onsubmit=&quot;return f();&quot; METHOD=&quot;Post&quot;.....>



Can I access m in function f?

Any help/suggestions will be appreciated. Thank you.
 
check out the faqs !!! everything is in it !!! so i won't detail my answer ....
(what you wrote won't work tho : what about the cfoutputs !!!!!
function f()
{
alert(&quot;Message is&quot; + <cfoutput>#m#</cfoutput>);
}
and remember that cf is SERVER side whereas jscript is CLIENT side - this should explain you where to put the cfset ...(NOT where it is) ;-) )
 
Thanks a lot Iza...but it seems that there is still something that I am not doing quite right.
The <CFOUTPUT> around the variable doesn't work.
I understand that CF is Server side. Where should I set the cf variable? Should the <CFSET> be within the <FORM> </FORM> tags?
 
Hey Kush,

When you look at the page in the browser with a view source, what does the &quot;alert(&quot;Message is&quot; + <cfoutput>#m#</cfoutput>)&quot; line look like?

GJ
 
Kush, try this:

<SCRIPT>
function f()
{
alert(&quot;Message is&quot; + document.MyForm.m.value);
}
</script>

<CFSET m = &quot;Hello World&quot;>
<CFFORM onsubmit=&quot;return f();&quot; METHOD=&quot;Post&quot; name=&quot;MyForm&quot; action=&quot;test.cfm&quot;>
<input type=&quot;hidden&quot; name=&quot;m&quot; value=&quot;<cfoutput>#m#</cfoutput>&quot;>
<input type=&quot;submit&quot;>
</cfform>

A good way to store a CF variable is in a hidden form field, because Javascript can access these fields and thus use them. Hope this helps...



<webguru>iqof188</webguru>
 
iqof is right
but if you don't want to pass the var this should be working

<cfset m=&quot;hello&quot;>
<script language=javascript>
function test() {
alert(<cfoutput>&quot;#m#&quot;</cfoutput>)
...
 
hey iza and iqof188....thanks a lot. Passing the variable through a hidden form field did the trick!

I was also able to achieve results using the following as suggested by iza.
<cfset m=&quot;hello&quot;>
<script language=javascript>
function test() {
alert(<cfoutput>&quot;#m#&quot;</cfoutput>)
...
(It didn't work earlier because I missed the quotes around #m#)

I appreciate your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top