Animated in-progress indicator for long running pages
On pages that take more than a few seconds to complete, its a good idea to show some type of in-progress meter. Things like bulk email processing and large file uploads are good candidates. Often this doesn’t need to be as complicated as a real progress meter, where you show the actual percentage complete. Instead, an “in progress” meter just gives feedback to the user to let them know that something is going on.
I usually place a hidden element on the page, then use JavaScript to make it appear when the form is submitted. Something like:
<p style="visibility:hidden" id="inprogress">
<img id="inprogress_img" src="progress.gif">
Please Wait...
</p>
<input type="submit" onclick="document.getElementById('inprogress').style.visibility='visible'">
Note that there is an image used in the in-progress message, this is important. A static “Please Wait” message tells your user they should wait, but without the feedback of the moving image, they could still get the impression something is locked up. You can get some awesome free progress meters from http://www.ajaxload.info.
Of course Internet Explorer throws a wrench into things. IE will stop any animated gifs from animating when the form is submitted. This is a problem in IE6 and 7. This is far worse than having no progress meter, since it appears as if something has locked up. But there is a work around. We just need to kick start the animation after the form submits. We do this by resetting the source of the image with JavaScript. We have to reset the src after the form submits, and the form submits when our function returns. So we make use of the setTimeout function, which executes a line of JavaScript after a specified number of milliseconds. We’ll also throw in an anti-double submit feature similar to what I mentioned in my last post.
<script language="JavaScript">
var submitted = false;
function doSubmit() {
if (! submitted) {
submitted = true;
ProgressImg = document.getElementById('inprogress_img');
document.getElementById("inprogress").style.visibility = "visible";
setTimeout("ProgressImg.src = ProgressImg.src",100);
return true;
}
else {
return false;
}
}
</script>
<input type="submit" onclick="return doSubmit()">
If your form is long enough that it needs scroll bars, be sure to place the in-progress message where the user will see it when they click the submit button.
March 7th, 2007 at 2:52 pm
Good solution. Thank You.
One small issue. To get this to work, you need to set the id=’inprogress_img’ in the img tag as in:
img id=”inprogress_img” src=”progress.gif”
March 7th, 2007 at 3:03 pm
Good call, I missed that in my example. Its been updated now.
March 26th, 2007 at 8:14 am
Thanks for this very good solution. No thanks to IE and all of it’s headaches. :X
April 10th, 2007 at 1:59 pm
Perfect. Worked like a charm!
July 28th, 2007 at 8:50 pm
I’m wanting to do something like this also, but instead of submitting a form I’m using a
December 10th, 2007 at 5:16 pm
Nice solution! Glad you found a way around this problem.
January 6th, 2008 at 4:41 am
Nice, but how (where) do I hide the progress image?
March 14th, 2008 at 4:02 am
Hi can i know how to hide the progress image when the processing is over? Thanks!
June 27th, 2008 at 11:08 am
Thanks ! Can you let me know, if there is a way to keep the position of the image dyanmic? next to cursor/ mouse pointer ?
July 1st, 2008 at 10:18 am
Does this script work with all browsers and latest versions.