DEV Community

James Moberg
James Moberg

Posted on

Searching the CFML Output Buffer for a String

In one of our projects about 10 years ago, we were fetching HTML fragments from a trusted third-party for inclusion on the website. Instead of using an iFrame or incorporating ajax, which could negatively impact our SEO, we would occasionally fetch the content in the background and update the webpage. While this approach worked fine, there were times when the fragments would contain some inline JavaScript, but wouldn't work because the JavaScript library wasn't loaded... or if loaded within the HTML fragment would cause a problem because now the JS library is being loaded more than once.

To work around this, we wrote some code that checks the existing java output buffer to determine if a string exists or not. For example, if the HTML fragment used jQuery, we'd use streamFind("jquery-") to identify whether the jQuery JS library was already included in the output stream. The UDF returns a boolean response and we'd include script tags to load the jQuery JavaScript library if it returned false.

We've used this UDF for many years, haven't encountered any issues and it's been extremely useful. It's compatible with Adobe ColdFusion, Railo and Lucee CFML. Enjoy!

Source Code

https://gist.github.com/JamoCA/3f5f041f2ca5bc0a5358597a5d78c91f

<!--- streamFind UDF (2015-06-09) Searches the output buffer to determine if a string exists. Works w/Adobe ColdFusion, Railo & Lucee CFML.
GIST: https://gist.github.com/JamoCA/3f5f041f2ca5bc0a5358597a5d78c91f
BLOG: https://groups.google.com/g/railo/c/Q4KV0yT7oGk/m/hgLXXMOWtBgJ
TWEET: https://x.com/gamesover/status/1834384722614780095
INSPIRATION: Michael Offner/Railo back in 2012. https://groups.google.com/g/railo/c/Q4KV0yT7oGk/m/hgLXXMOWtBgJ
--->
<cfscript>
boolean function streamFind(required string str, boolean caseSensitive=false) output=true hint="I search the output buffer to determine if a string exists" {
if (arguments.caseSensitive){
return (listfindnocase("railo,lucee", server.ColdFusion.ProductName)) ? getPageContext().getOut().getString().indexOf(arguments.str) neq -1 : getPageContext().getCFOutput().getBuffer().toString().indexOf(arguments.str) neq -1;
}
return (listfindnocase("railo,lucee", server.ColdFusion.ProductName)) ? getPageContext().getOut().getString().toLowerCase().indexOf(lcase(arguments.str)) neq -1 : getPageContext().getCFOutput().getBuffer().findStringNoCase(arguments.str) neq -1;
}
</cfscript>
view raw streamFind.cfm hosted with ❤ by GitHub

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay