HOME | FERGUSON Digital Blog

UDF to Compact HTML

ColdFusion Add comments

Savvy web developers who are always looking for ways to reduce their web clutter may want to compact the view of their client side product. Although it could be argued that compacting the HTML side may reduce the amount of "benchmarking" performed by an over enthusiastic newbie, for ColdFusion writers having someone being able to clearly read the HTML side does little to obfuscate their endeavors, it's only the HTML after all.

There are still a great many people surfing the web with slow connections. There are still dial-up modems out there, but these days I'm just referring to cell phones and slow reception.

A great many actions can be performed to minimize the footprint of a web application; reduce the number of graphics, simplify those graphics, reduce the fidelity of the graphics used, save the showboating for large projects (Adobe Flash, complex Cascading Style Sheets, remoting, etc.), but compressing the HTML can make a contribution as well.

Pick your favorite spot to store the User Defined Function and call it when delivering long pages, compressing a short page really won't make any difference at all for obvious reasons.

<cfscript>
   function fnCompactHTML(ObjectHTML) {
      ObjectHTML=REReplace(ObjectHTML, "[[:space:]]{2,}", " ", "ALL");
      ObjectHTML=REReplace(ObjectHTML, "/\*[^\*]+\*/", " ", "ALL");
      ObjectHTML=REReplace(ObjectHTML, "[ ]*([:{};,])[ ]*", "\1", "ALL");
      return ObjectHTML;}
</cfscript>


This example takes the finished page when called, passing the variable of a CFSAVECONTENT to the UDF works particularly well. The UDF itself is just a set of three Regular Express arranged to remove spaces, tabs and linefeeds from the HTML client-side of the page (does not affect the CFM page) to deliver a compacted view of the HTML to the browser.

The page will display exactly the same, compressed or not, this is because the tabbed (or spaced) code encapsulation we do is for our benefit. Reading, modifying, and continued development on non-compressed code is a lot easier for a human to read than a paged compressed to one continuous line without breaks, tabs, or spaces! Browsers don't have that problem and don't need the extra content to display the end result

If you find this post useful please leave a comment and let me know how you used the information.

0 responses to “UDF to Compact HTML”

Leave a Reply



© Copyright 1997-2024, All Rights Reserved Coldfusion and MS SQL2008
Powered by Mango Blog.   Design by FERGUSON Digital
o:Event name="beforeHtmlBodyEnd" />