How can I display a message on a long-running page?
By default, ColdFusion will not return any HTML until the entire page has rendered. For a long-running page, this may make the user think nothing is happening, resulting in the user hitting reload multiple times.
ColdFusion provides a tag that will flush out the current data to the screen: <cfflush>. For example:
This is a slow page, please stand by...
</p>
<cfflush>
<cfloop index="x" from="1" to="999999">
<cfset doNothing = x*2/3>
</cfloop>
<p>
I'm finally done.
</p>
In this example, the user will see the "please stand by" warning immediately. After the page has processed they will then see the final message. A few warnings about <cfflush>. When used - the following tags and functions may no longer be used: <cfcontent>, <cfcookie>, <cfform>, <cfheader>, <cfhtmlhead>, <cflocation>, and SetLocale(). Also, some browsers, like the wonderful Internet Explorer, will ignore your text unless you have "enough" text. If you see nothing in Internet Explorer, you can simply pad your output by adding: repeatString(" ", 100). This fools the browser into thinking enough text has been sent to render.
This question was written by Raymond Camden.
It was last updated on February 23, 2006 at 7:11:59 AM EST.
CFML Referenced
<cfflush>
SetLocale()
<cfheader>
<cfcontent>
<cfform>
<cfhtmlhead>
<cflocation>
<cfcookie>
<cfloop>
Categories
Comments
Comment made by walt on February 23, 2006 at 10:26 AM
Whoa, I was just going to ask you about doing something like this! w00t!
Comment made by john smoltz on February 23, 2006 at 10:42 AM
Is there anyway to know how far along a cfcontent tag is to completion?
Comment made by Paul Carney on March 1, 2006 at 11:42 AM
The limitations listed by Ray are real and for most of our scripts, a case where we cannot use <cfflush>.
We have a special process that includes a database table of process statuses, keyed by a UUID. We then have a special "please wait" page that shows something nice and uses the META refresh command to check the status every x seconds.
When the process that is running is done, it updates the row for that UUID in the status table and the next "please wait" page refresh finds this flag and sends you to whatever page is next.
It does require extra threads, but is the only way we found to keep a person updated with the status of a background job. We even have one process (our geo-locator auto-scheduler for people/locations) that will display updated results with each refresh, so that you can see how far along the job is.
Comment made by Joel Stobart on July 2, 2008 at 9:29 AM
I think this could now be done with cfthread? Kick off the long running thread and then use Paul Carneys approach (ish). - Joel
Comment made by Raymond Camden on July 2, 2008 at 9:45 AM
To be clear Joel - your talking about a way to help handle the slow process - but the main gist of the entry is the use of cfflush to get the message out to the user. You don't think the main entry needs changing, do you?