Thoroughly Document Your Use Of ColdFusion's CFHTMLHead Tag

Posted September 29, 2009 at 10:12 AM

Tags: ColdFusion

The other day on Twitter, I saw someone tweet about how they just discovered ColdFusion's CFHTMLHead tag and was planning to use it in a project. This gave me flash backs to a project I took over in which the CFHTMLHead tag was being used all over the place. Having not known about this tag at the time, I thought I was going crazy!! Stuff was showing up in the HEAD of the HTML document that wasn't there in the corresponding ColdFusion template and I couldn't for the life of me figure out where the heck it was coming from.

For anyone who has never seen the CFHTMLHead tag - please stop reading this post immediately - it takes text content and injects it into the end of the HTML HEAD element, assuming it has not yet been flushed to the client. For ease of use and demonstration, I am going to wrap the CFHTMLHead tag into a custom tag so that I can use the custom tag's generated content to set the CFHTMLHead text attribute:

htmlhead.cfm

 Launch code in new window » Download code as text file »

  • <!---
  • Check to make sure this is the end of the tag. This way, we
  • will have generated content to work with.
  • --->
  • <cfif (thistag.executionMode eq "end")>
  •  
  • <!---
  • Use the generated content for the data piped into
  • the CFHTMLHead tag.
  • --->
  • <cfhtmlhead text="#thistag.generatedContent#" />
  •  
  • <!---
  • Clear the generated content. We used it for the
  • CFHTMLHead tag and have no need to output it now.
  • --->
  • <cfset thistag.generatedContent = "" />
  •  
  • </cfif>

As you can see, the CFHTMLHead tag has one attribute, Text, which is the text output that will be injected into the HTML.

Now, let's take a look at how this works:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>ColdFusoin CFHTMLHead</title>
  • </head>
  • <body>
  •  
  • <h1>
  • ColdFusion CFHTMLHead Example
  • </h1>
  •  
  • <p>
  • The CFHTMLHead tag, while seemingly powerful, can be
  • extremely hard to debug if not docummented well.
  • </p>
  •  
  • </body>
  • </html>
  •  
  •  
  • <!--- Add content to the head. --->
  • <cf_htmlhead>
  •  
  • <script type="text/javascript">
  • document.write( "Where did I come from?!?" );
  • </script>
  •  
  • </cf_htmlhead>

As you can see here, after the HTML we add a Javascript tag using the CFHTMLHead custom tag wrapper. When we run the above page, here is the page source that results:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>ColdFusoin CFHTMLHead</title>
  •  
  • <script type="text/javascript">
  • document.write( "Where did I come from?!?" );
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • ColdFusion CFHTMLHead Example
  • </h1>
  •  
  • <p>
  • The CFHTMLHead tag, while seemingly powerful, can be
  • extremely hard to debug if not docummented well.
  • </p>
  •  
  • </body>
  • </html>

As you can see, the Javascript defined at the end of the page winds up in the HEAD tag of the rendered content. While this might have some ooh-ahh factor at first, realized that this was a very simple example; imagine yourself in the middle of a massive application with hundreds of templates! Suddenly, having things pop up in the HEAD tag out of nowhere will not only be confusing, but very hard to debug.

This is one of the few ColdFusion tags that I actually recommend people don't use. It just seems very hacky to me - like using it is a symptom of some flaw in your page architecture. However, if you are going to use it, I beg of you this one thing: document your use of it! Either put some sort of ColdFusion comment in your HEAD tag about where content might come from; or, even put a comment in your CFHTMLHead content:

 Launch code in new window » Download code as text file »

  • <!--- Add content to the head. --->
  • <cf_htmlhead>
  •  
  • <!-- Content injected using CFHTMLHead. -->
  •  
  • <script type="text/javascript">
  • document.write( "Where did I come from?!?" );
  • </script>
  •  
  • </cf_htmlhead>

Regardless of how you go about it, make sure that someone can look at either the ColdFusion page template or your rendered page source and gain some insight as to where this content is coming from. Otherwise, you'll be kicking yourself down the line (or get kicked by the developer that took over your project).

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Print Page




Reader Comments

Sep 29, 2009 at 10:32 AM // reply »
45 Comments

I love cfhtmlhead. I use it religiously in my Model-Glue apps. To me it makes more sense to keep my JavaScript logic in my view instead of mucking about trying to fit it all in my massive dspTemplate.cfm file and playing the viewstate.setValue('needSomeScript', true) game.

Different strokes for different folks I guess ;)


Sep 29, 2009 at 10:36 AM // reply »
6,516 Comments

@Todd,

As long as you document, it's totally fine. However, I wouldn't refer to setting things into the ViewState a "game" :) after all, it's there to carry data throughout the page request.


Sep 29, 2009 at 11:06 AM // reply »
9 Comments

It might also be a good idea to warn people that CfHtmlHead is immune to good old >cfcontent reset=true<. I had issues with some old code where I wanted to clear the output buffer and kept finding out it wasn't empty. I then finally hunted down the culprit CfHtmlHead.


Sep 29, 2009 at 11:10 AM // reply »
6,516 Comments

@David,

Very interesting. So, no matter what you do, the CFHTMLHead data gets injected.

Also, in the "Web 2.0" world, I think it's worth noting perhaps that it is recommended that a lot of linked content (ie. Javascript) be placed at the bottom of the page such that the UI can load faster.


Sep 29, 2009 at 2:19 PM // reply »
27 Comments

I've not tested it, but seems like this tag might break when the head tag has profile="" attribute. http://stackoverflow.com/questions/1477266/how-to-prevent-coldfusion-from-injecting-cfform-js-into-the-head-section


Sep 29, 2009 at 3:24 PM // reply »
1 Comments

I use CFHTMLHead to add stylesheets when a page is being built by our CMS pulling the content out of our database, we have one global one that is always used and then depending on what kind of content a user has added it will insert the correct stylesheets at the top of the datatypes cfm template.
since it doesn't really make sense to use it before closing your head tags maybe even just putting a HTML comment just before the </head> would make it easier to see what is from where


Sep 29, 2009 at 6:07 PM // reply »
12 Comments

I agree with Ben that use of cfhtmlhead should be avoided. I ended up writing up my own custom tag that copies text passed to it as a key in request scope. Since I use a central layout (MG & Fusebox), the layout file extract that key and spits it out before </head>


Sep 30, 2009 at 4:45 AM // reply »
1 Comments

That's not all.
Using <cfhtmlhead> can also bite you in the a$% if at some point you have to use <cfflush> to partially serve large files and one of the "head" calls happens after the "flush".


Sep 30, 2009 at 6:28 AM // reply »
9 Comments

I've found what looks like the best way to reset/clear anything the CfHtmlHead tag has set. Model-Glue has the best version I've seen of this approach, it works around a bug in other versions and has Railo/BlueDragon support too.

It's called "resetCfHtmlHead" and can be easily dropped into any existing code by just copying out the function itself.
http://svn.model-glue.com/tag/MG_3.0_Final%20_8_10_2009/ModelGlue/gesture/remoting/AbstractRemotingService.cfc


Oct 1, 2009 at 8:09 AM // reply »
6,516 Comments

@David,

Cool stuff. Definitely a good understanding of the internal workings of the page request / response mechanisms.


Oct 5, 2009 at 2:46 AM // reply »
4 Comments

Hi,
This is very helpful and informative article.You have given some nice code and suggestion for the CFHTML use.


Oct 8, 2009 at 4:16 AM // reply »
9 Comments

I've managed to piece together the following code from various sources on the internet, allowing you to reset/clear or get the contents of the cfhtmlhead buffer. It works in ColdFusion only (no railo/bluedragon) and comes in quite handy if dealing with code using CFHtmlHead.

http://pastebin.com/f2560e44d


Oct 12, 2009 at 3:04 PM // reply »
10 Comments

@David: What version of ColdFusion did you test that with? I am on 8.0.1 and the getCFHtmlHead function errors on the line with local.out.getClass().getDeclaredField("headerBuffer"). It appears that my version of ColdFusion doesn't have a headerBuffer field in the NeoBodyContext class, but insead an appendHeaderBuffer and prependHeaderBuffer field. Interestingly enough, the contents of cfhtmlhead goes into appendHeaderBuffer and the JavaScript produced by cfajaxproxy shows up in prependHeaderBuffer. I guess this is the problem when dealing with uncoumented features of your app server. :)


Oct 13, 2009 at 11:22 AM // reply »
9 Comments

@Brad,

Well spotted! I've been doing this in 7 but when I tested it on 8 I forgot to remove a try/catch I had added.

I'll have a look at that and see if I can sort it out. ;)


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »