Using ColdFusion for Dynamic Server-side JavaScript

Posted By: Brian Meloche; Posted At : February 12, 2007 3:08 PM

Related Categories: ColdFusion, JavaScript, AJAX

Here's a little tip for using a .cfm file for dynamic JavaScript generation.

Need to use JavaScript, but not sure what the JS will actually look like until you build it... but don't want to show the JavaScript on the webpage? I am using Spry for some Ajax style selects, but I need to control the XML on the fly.

Try something like this:

<cfparam name="url.year" default="#year(Now)#">
...
<script type="text/javascript" src="/ClientIncludes/DynamicCFJS.cfm?year=#url.year#"></script>

You could then have in your DynanicCFJS.cfm something like this:

<cfsetting enablecfoutputonly="yes" showdebugoutput="no">
<cfoutput>
var dsYears = new Spry.Data.XMLDataSet("/index.cfm?event=buildYearList&year=#url.year#", "carlineYears/year", { useCache: true });
</cfoutput>

Of course, similar techniques will work in any web language. Tailor to the web language of your choice.

Comments
I prefer your method. If for no other reason than because it discourages any future developer from adding anything between the two lines (as they could with the other method).

If you intend that init() should be called upon instantiation, every time, then write your code to reflect that.

Your method (which I use as well) ensures that no-one will try to use the component before it is fully intialized and allows all other code in that component to assume that init() has been run.
# Posted By dave | 4/22/08 7:04 PM
constructors in other languages return something... but it's built into the language:

Class myClass = new Class();

The "new" keyword assigns the instance.

In CF we make our own constructors. Any CF developer not using an init() with <cfreturn this/> is violating best practices and would have a very difficult time convincing me why going against them is a good idea. Frameworks like ColdSpring rely on the fact that developers adhere to best practices... if you have a CFC with an init() method that doesn't return itself it would be incompatible with ColdSpring.
# Posted By dross | 4/22/08 7:05 PM

Hosting provided by...

Hosting provided by hostmysite.com

Calendar

<< February 2012 >>

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

Archives By Subject

  • About Me (44) [RSS]
  • ActionScript (1) [RSS]
  • Actuate (1) [RSS]
  • Adobe (18) [RSS]
  • Adobe Community Professionals (3) [RSS]
  • AIR (1) [RSS]
  • AJAX (1) [RSS]
  • Animals (11) [RSS]
  • Articles (2) [RSS]
  • ASP (1) [RSS]
  • Blogging (14) [RSS]
  • BlueDragon (2) [RSS]
  • Canadian-American (1) [RSS]
  • CFConversations (16) [RSS]
  • Cleveland (3) [RSS]
  • Cleveland AUG (21) [RSS]
  • Cleveland CFUG (24) [RSS]
  • ColdFusion (81) [RSS]
  • Conferences (33) [RSS]
  • Design (3) [RSS]
  • Dreamweaver (1) [RSS]
  • Eclipse (1) [RSS]
  • Flash (2) [RSS]
  • Flex (10) [RSS]
  • Frameworks (7) [RSS]
  • Fusebox (1) [RSS]
  • J2EE (1) [RSS]
  • JavaScript (1) [RSS]
  • Jobs (2) [RSS]
  • LightFront (6) [RSS]
  • Linux (2) [RSS]
  • Mac (3) [RSS]
  • Mach-ii (9) [RSS]
  • Macromedia (9) [RSS]
  • MAX (34) [RSS]
  • Notes to Self (1) [RSS]
  • Open Source (7) [RSS]
  • Personal (17) [RSS]
  • Podcasts (23) [RSS]
  • Politics (1) [RSS]
  • Railo (0) [RSS]
  • Random Musings (10) [RSS]
  • Reality TV (1) [RSS]
  • Social Networking (1) [RSS]
  • Speaking (7) [RSS]
  • Subversion (2) [RSS]
  • Tech News (2) [RSS]
  • Technology (2) [RSS]
  • The Event (1) [RSS]
  • Tutorials (1) [RSS]
  • User Groups (23) [RSS]
  • Videos (1) [RSS]
  • Weight Loss (4) [RSS]
  • Windows (0) [RSS]
  • Work (8) [RSS]
  • Search

    Twitter Updates
    Follow me!

      RSS

      Subscribe

      Enter your email address to subscribe to this blog.

      Tags

      about me adobe animals blogging cfconversations cleveland aug cleveland cfug coldfusion conferences flex max personal podcasts random musings user groups

      Recent Comments

      Change a comment in an SVN log (or fixing a malformed SVN log file after changing a comment)
      Brian Meloche said: @HippyJim, that's a good point, or at least choose an editor that can write in Unix CR/LF format. Do... [More]

      Change a comment in an SVN log (or fixing a malformed SVN log file after changing a comment)
      Ellaine Matthews said: Does the same solution apply to the new problem of SVN? Thanks a lot for this post! [More]

      Change a comment in an SVN log (or fixing a malformed SVN log file after changing a comment)
      freelance writing jobs said: That is great! I first hear about it! [More]

      Change a comment in an SVN log (or fixing a malformed SVN log file after changing a comment)
      Hippyjim said: Thanks for the tip - it's also worth noting that if your SVN repo is stored on a linux / unix server... [More]

      End of Another Era: Changes at the ColdFusion Helm
      Brian Meloche said: Tim Cunningham makes a couple of points on the subject worth reading over on his blog: http://cfmum... [More]

      Podcasts I Listen To...

      Active Podcasts
      CFConversations (My Podcast)
      CFPanel (technically not a podcast, but close enough!)
      RIA Podcast
      CFHour()
      The Flex Show
      Non-active CF/Flex Podcasts (still worth a listen)
      ColdFusion Weekly
      The ColdFusion Podcast
      Helms and Peters Out Loud
      Other Podcasts
      This Week in Tech (TWiT)
      Windows Weekly with Paul Thurotte
      MacBreak Weekly
      Net @ Nite
      Slashdot Review
      Ruby on Rails Podcast (I'm not a Rails developer, but it's always a good idea to stay in touch with the competition!)
      Web Design Podcast from boagworld.com
      Practical Web Design Magazine