|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
passing coldfusion variable to javascriptI have an external javascript file which I have attached to my page. I want to set some variables in this file to corresponding CF variables. How can I do this? I tried: --------- <html> <cfoutput> <head> <script src="my_own.js"></script> </head> </cfoutput> <body> my web page </body> </html> ----- but the CF code (e.g. #my_variable_name#) inside the my_own.js file remains unchanged. Any ideas? Thank you ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328109 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.4 |
|
|
Re: passing coldfusion variable to javascriptWell, obviously, JS is client side code and CF is server side. Normally if I wanted to let JS work with a CF variable, I'd simply output it: <cfoutput> <script> var x = #x# </script> </cfoutput> or <cfoutput> <script> var #toScript(x, "x")# </script> </cfoutput> However, in your case, the JS library isn't emdedded on the page, but loaded externally. So your options then are a bit different. You could put the CF variable in a hidden form field and just read it that way. That would be OK for simple variables. Another option is to use AJAX to fetch the value. On Sat, Nov 7, 2009 at 5:28 AM, Jayel Villamin <jayel.villamin@...> wrote: > > I have an external javascript file which I have attached to my page. I want to set some variables in this file to corresponding CF variables. > > How can I do this? > > I tried: > --------- > <html> > <cfoutput> > <head> > <script src="my_own.js"></script> > </head> > </cfoutput> > <body> > my web page > </body> > </html> > ----- > > but the CF code (e.g. #my_variable_name#) inside the my_own.js file remains unchanged. > > Any ideas? > > Thank you > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328111 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.4 |
|
|
Re: passing coldfusion variable to javascript<html> <!--- assume the javascript file my_own.js contains the following script. You could have put it in a script tag on the page. The web server will deliver the src file as though it were included on the page. So anything that appears in the src file will be available to the browser. var declaredvar; function calledfunc(var calledvar){ return declaredvar + calledvar; //cat the two strings } ---> <cfset cfvariable="hello" /> <head> <script language="javascript" src="my_own.js"></script> <script language="javascript" var returnvar; // set the value of the variable, in this case to a string // but it can be any datatype, javascript is not heavily typed. declaredvar='<cfoutput>#cfvariable#</cfoutput>'; // use the passed variable in your javascript file, as it can see it returnvar = calledfunc(' world'); // display hello world document.write returnvar; </script> </head> <body> my web page </body> </html> The thing to remember is that cf is processed on the server, and javascript is processed on the client. The script tags is processed on the server in that it renders it to the page when it is output. The actual function calls, etc. are processed by the browser. You already knew this I am sure :o) So what happens is that cf initializes the the declaredvar to the value referenced by the <cfoutput> (hello), then the browser calls calledfunc('hello'), which returns 'hello world'. The browser then renders 'hello world' to the page. If you wanted to use the returned value to further process in cf, that is much trickier. I will leave that to other's smarter than me on this thread. Nick ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328115 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.4 |
|
|
RE: passing coldfusion variable to javascriptAlso, if the JS file is on the same server as the processing page you could simply read in the JS file, replace the variables with their ColdFusion equivalents and go from there. -----Original Message----- From: Raymond Camden [mailto:rcamden@...] Sent: Saturday, November 07, 2009 7:52 AM To: cf-talk Subject: Re: passing coldfusion variable to javascript Well, obviously, JS is client side code and CF is server side. Normally if I wanted to let JS work with a CF variable, I'd simply output it: <cfoutput> <script> var x = #x# </script> </cfoutput> or <cfoutput> <script> var #toScript(x, "x")# </script> </cfoutput> However, in your case, the JS library isn't emdedded on the page, but loaded externally. So your options then are a bit different. You could put the CF variable in a hidden form field and just read it that way. That would be OK for simple variables. Another option is to use AJAX to fetch the value. On Sat, Nov 7, 2009 at 5:28 AM, Jayel Villamin <jayel.villamin@...> wrote: > > I have an external javascript file which I have attached to my page. I want to set some variables in this file to corresponding CF variables. > > How can I do this? > > I tried: > --------- > <html> > <cfoutput> > <head> > <script src="my_own.js"></script> > </head> > </cfoutput> > <body> > my web page > </body> > </html> > ----- > > but the CF code (e.g. #my_variable_name#) inside the my_own.js file > > Any ideas? > > Thank you > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328116 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.4 |
| Free embeddable forum powered by Nabble | Forum Help |