Thursday, March 24, 2005

Limitation of Unobtrusive JavaScript?

Unobtrusive JavaScript
Mike Rankin's MX Blog: Unobtrusive JavaScript

I was looking at the provacative article about unobtrusive JavaScript and realized that there is a limitation based on how I wrote some of my older JavaScript.

If you use ColdFusion to populate values in your JavaScript, moving your code out to .js files prevents the cf engine from processing them.

There are many instances where I use a form control event to trigger a JavaScript function that accepts a parameter that is generated by ColdFusion. How would this work if the event is not triggered from the .cfm page?

Are there elegant ways to handle this? Is there a way to make the cf engine parse a .js file? Can the JavaScript code be changed to make it work with the unobtrusive approach?


  1. Hi Mike,

    Here's what I do. I simply change

    <script type="text/javascript" src="scripts.js">


    <script type="text/javascript" src="scripts.cfm">

    And then (obviously) rename change the file extention of my js file to cfm.

    Works like a charm for me.

  2. Doh! Sometimes a solution just stares you in the face and laughs at you.

    BTW, Trond, Fantastic work on the mach-ii tutorials.

  3. Strange, this doesn't seem to work for me. Could be my architecture is not playing nice with the process. Plus, DW doesn't realize it's dealing with a js file.

    Cfinclude seems to work more reliably, but then your code is in the resulting page, which seems to go against the premise of the unobtrusive javascript.

  4. Did you add a cfheader with a type of text/javascript to the script.cfm file? I also tend to name the files .js.cfm that way I know it is a cfm file that is actually acting like a javascript file.

  5. Thanks John,

    I hadn't tried that. To be honest, I kinda gave up on the unobtrusive javascript. I really only use js for a few UI effects and client side data validation. Unfortunately, I suffer from trying to pour a milk bottle full of knowledge into a shot-glass of a brain. Most of it spills over and some of what was there before gets pushed out to make room for new stuff.