Skip to main content
Ben Nadel at cf.Objective() 2012 (Minneapolis, MN) with: Shawn Slaughter and Scott Stroz
Ben Nadel at cf.Objective() 2012 (Minneapolis, MN) with: Shawn Slaughter Scott Stroz

Checking To See If CFFlush Has Already Been Executed

By
Published in Comments (4)

I just posted this on the House of Fusion CF-Talk list, but I thought I would posted up here. Someone has asked if there was a way to determine if the CFFlush tag had already been executed. I am sure there is a way to get this by digging around in the GetPageContext() family of objects, but you can perform this simply by trying to set a header value:

<cftry>
	<!--- Try set a status code. --->
	<cfheader
		statuscode="200"
		statustext="OK"
		/>

	<!---
		We set the status code, flush has
		not been called.
	--->
	<cfset REQUEST.FlushInitiated = false />

	<cfcatch>

		<!---
			The CFHeader tag has thrown an error. CFFlush
			has already been executed.
		--->
		<cfset REQUEST.FlushInitiated = true />

	</cfcatch>
</cftry>

CFHeader tags can only be executes prior to content being sent to the browser since header information is the first thing that is sent. If content has already been flushed to the browser, then CFHeader will throw an error upon execution. In this example, we CFTry to execute a CFHeader tag. If it works, then no content has been sent to the browser, and we set our flag (REQUEST.FlushInitiated) accordingly. If an error is thrown by the CFHeader tag, then we know CFFlush has been executed and we set our flag accordingly.

If you want to do this with out having to set a variable, the easy solution would be to just wrap it in a ColdFusion user defined function (UDF):

<cffunction
	name="IsFlushInitiated"
	access="public"
	returntype="boolean"
	output="false"
	hint="Determines if a CFFlush tag has been executed yet.">

	<cftry>
		<!--- Try set a status code. --->
		<cfheader
			statuscode="200"
			statustext="OK"
			/>

		<!---
			We set the status code, flush has not
			been called.
		--->
		<cfreturn false />

		<cfcatch>

			<!---
				The CFHeader tag has thrown an error. CFFlush
				has already been executed.
			--->
			<cfreturn true />

		</cfcatch>
	</cftry>
</cffunction>

Then you can just call this like:

<!--- Check to see if CFFlush has been called. --->
<cfif IsFlushInitiated()>
	....
</cfif>

Want to use code from this post? Check out the license.

Reader Comments

20 Comments

Throwing an exception as regular program flow is far from best practice and can also be quite expensive in some situations. This method also has the unintended side effect that checking if the buffer has been flushed resets the response code to 200 which may be undesirable if a different part of the application had explicitly set the response code to something else previously.

You're right though, there is a PageContext method! :)

getPageContext().getResponse().isCommitted()

ref: http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletResponse.html#isCommitted()

On a side note. If for some reason you *cannot* use getPageContext(), for instance if you're on CF5, an alternative method would be:

<cfheader name="CFFlushInited" value="false">

which will throw an exception just like your code, but won't overwrite potentially valuable response information.

15,841 Comments

@Elliott,

Good point; throwing exceptions is not a good practice. But to be fair, checking to see if CFFlush has been executed is probably not a best practice either :)

Thanks for pointing out a better way to perform this task. I think I saw something like this in FireBug or something (Ray Camden).

As far as an alternate method for CF5... what is CF5? ;)

Silly people still using CF5....

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel