08 October 2004

Please Wait... buttons

Dispite Broadband, the web is still slow for some people. Other people are just impatient. Have you ever suffered the wrath of an impatient clicker? Here is a technique that might help reduce the clicks.

Picture this;

You are a normal web user. By this I mean that you are not a web developer, and you are not amazingly web savvy. You are trying to find some information on the Internet about fishing, so you go onto your local fishing web forums, and spend quite a bit of time filling out a form to post a new message. When you have finished you scroll to the bottom of the page and click the ‘Post Message' button. After a couple of seconds nothing seems to happen, so you click it again. Still no immediate response, so you click is a few more times. Eventually the page submits, and you have either posted the same message 20 times, or generated an error of quite seriousness. Either way, to some web developer in the world, you have just become the bane of his day!

A 'Please Wait...' button It's time to fight back at our impatient click-happy enemy! Or at least try and help them.

Back to reality

It's not their fault, but users like the one above can be a real pain. Impatient and ‘click happy' is the way a lot of people interact with the web. You may or may not have had to deal with someone who has done this, but inevitably you will have to. Unless…

Pro-active buttons

The solution as I see it is either to stop people from clicking the buttons more than once, or to let them know that they only need to click it once.

My idea came when I notice what Alex Haneng had done on his website forums ( www.haneng.com/forums.asp ). In his forms, he has buttons coded with an OnClick event:

OnClick="ButtonName.disabled=true;submit();" 

It was a good idea I thought, even if it was implemented in a bad way. Basically, as soon as someone clicks the button, it will disable the button so that they can't go click happy and click again.

I thought that with a little bit of improving, that the idea could be taken to new levels.

Introducing the DOM version

I wanted to use the same idea as Alex, but without introducing any javascript into my buttons. My DOM scripting has been getting a bit better polished, so I decided to give it a go with DOM based Javascript.

In the system that I wanted to implement this onto, all my buttons (INPUT tags with type=”sumit”) also have the class of ‘button'. This is for my style sheet, but can also be used in my javascript.

The plan is to loop through all the INPUT tags on the page, find the ones with a ‘button' class, and add an onclick event to them.

pleaseWaitButtons = function(){
	//find all the INPUT tags
	var navRoot = document.getElementsByTagName("input");
		//loop through all the tags and find the ones with class=”button”
		for (var i = 0; i <  navRoot.length  ; i++)  {
			if (navRoot[i].className=='button'){
				//add the onclick event
				navRoot[i].onclick = function(){
					this.disabled=true;
				}//end onclick function
			}// end if
		}// end for loop
}// end function
window.onload=pleaseWaitButtons;

View Demo One

Now, when someone clicks one of my buttons, they can't possibly click it again.

But wait, it's not perfect; If a user decides straight away that they didn't want to click the button, and they hit the browsers ‘stop' button, then the disabled button because useless to them. Not accessibility.

Another problem; the system that I want to implement this into is an ASP.NET system. ASP.NET commonly uses multiple buttons on one form; if you disable the button the ASP.NET code behind page cannot work out what has been clicked and wont trigger off the correct server side ‘click' event. So, disabling the buttons isn't a good idea after all!

Please Wait…

After discovering the little ASP.NET incompatibility, I decided that what would be best is to leave the button enables, but to change it so that the user would see that something was happening. Wouldn't it be great I thought if I was to tell the use to ‘Please Wait…' by actually changing the text of the button to say just that! So I did;

pleaseWaitButtons = function(){
	//find all the INPUT tags
	var navRoot = document.getElementsByTagName("input");
		//loop through all the tags and find the ones with class="button"
		for (var i = 0; i <  navRoot.length  ; i++)  {
			if (navRoot[i].className=='button'){
				//add the onclick event
				navRoot[i].onclick = function(){
					this.value='Please Wait...';
				}//end onclick function
			}// end if
		}// end for loop
}// end function
window.onload=pleaseWaitButtons;

View Demo Two

As you can see, only one line needed to change. I thought to myself that this would do. If for any reason the submission of the form is taking a long time, the button will say ‘please wait…' and the user will know that something is actually happening. No need to keep on clicking. A nice usable, and accessible way of providing extra functionality to help users!

But I forgot something; What if the button doesn't post you to another web page. For example; in my ASP.NET application there is a section where you can request a bunch of XML files to be downloaded in a ZIP file. We have a form that lets you select the files you want in the ZIP, and then a button that will post you to another ASP.NET page that builds the ZIP file on the fly, and streams it to the users browser. This means that you never actually leave the current page; you just get a prompt to download the ZIP file. What's the problem? Well the text of the button just clicked says ‘Please Wait…' even after the desired events has been triggered off. I needed to find a way of setting the button's text value back to the original after a certain amount of time.

Enter SetTimeout()

SetTimeout is a JavaScript function that I have used a little bit in the past. And then I realised my need, I thought of it straight away. What I needed to do was use this function to set a period of time before calling another function that would set the text value of the button back to normal. Easy peasey:

var origValue; //used to hold the value of a button just clicked
var currentButton;  //used to hold the DOM ref of the button

//the function used to reset the buttons after the ‘please wait…’ change
function resetValue(obj, value){
	obj.value = value;
}

pleaseWaitButtons = function(){
	//find all the INPUT tags
	var navRoot = document.getElementsByTagName("input");
		//loop through all the tags and find the ones with class=”button”
		for (var i = 0; i <  navRoot.length  ; i++)  {
			if (navRoot[i].className=='button'){
				//add the onclick event
				navRoot[i].onclick = function(){
					//set origValue to remember the original text value
					origValue =  this.value;
					currentButton = this;
					this.value='Please Wait...';
					//set the timeout for 10 seconds
					setTimeout("resetValue(currentButton, origValue)", 10000)
				}//end onclick function
			}// end if
		}// end for loop
}// end function

window.onload=pleaseWaitButtons;

View Demo Three

There you have it, my ‘please wait…' buttons! The most important parts in the code above is the resetValue function, which expects the values ‘obj' and ‘value', the first being the DOM path to the button, and the second being the original text value of the button (saved in the origValue variable – before changing the text to ‘please wait…'). I could have called the two variables (origValue and currentButton) from the function, without asking for them like above, but I felt it would be best to do it this way in case I ever added anything else to this JavaScript that might make use of the resetValue function.

I hope that you find this useful, happy wubbing!

Comments ( 5 )

  1. Malarkey

    Now THAT is the sort of article I have been waiting for matey... not tried the forms in anger yet, but top notch!
    08 October 2004 at 20:06
  2. Phil Baines

    Im glad you like it Malarkey! This isn't the article that I told you I wanted to write the other day. I just came up with the script in work on thursday and thought that a write up on the site would be good.

    I'm sure that it could be improved on, and if anyone has any improvements I would be more than happy to hear about them!
    09 October 2004 at 19:57
  3. Chris Neale

    Are you sure about `bain` ?

    http://dictionary.reference.com/search?q=bain

    The bane of my day/hour/'time unit of choice' is usually those that misspell or mispunctuate : )
    11 December 2004 at 23:33
  4. Phil Baines

    Thanks

    Thanks for the heads up Chris. I guess I have just gotten to used to everyone referring to me at the 'baines' of their life.
    13 December 2004 at 09:13
  5. James White

    Great Article

    This was exactly what I was looking for. Something simple and to the point without a whole lot of code to try and adjust/figure out. NIce job.
    16 February 2005 at 16:19

Sorry, commenting has been disabled for a while. I am getting a stupid amount of comment spam, and need to find a new way of doing things.

I will not publish your email address, but I may use it to get in contact with you.

HTML tags and entities display as source; they do not render. To create a live link, simply type the URL (including http://).

Topics

  • I am more than willing to admit the big gaps in my knowledge on many topics, including web development. This is a topic I use to get help from the community, and my imaginary audience.
    Ask the Audience
  • Even though looking pretty is not the most important layer to a succesful web site (I don't think there is a Most Important), it does help!
    Graphic Design
  • Have you got a sense of?
    Humor
  • The glam, the sadness, the joy, the madness.
    Life of Findel
  • What ever doesn't fit
    Misc
  • I have a crappy camera, but I do try. A good artist always blames his tools.
    Photography
  • Here I chat about projects I am working on, going to work on, or finished. Some of them I will be happy to let you in on, others I will be ashamed of.
    Project Watch
  • Site news and updates
    Site Info
  • Keepin' an eye on the wwwubb
    Watching The Web
  • This topic will be used for talking about all manner of project management issues relating to Wed Development (Wubb Development). Where we may talk about how to code in the Wubb Development topic, we will be more likely to talk about giving quotes, or planning projects, in this topic.
    Working with Wubbs
  • All things Web Development - Web standards, Hacks, Graphics, and more.
    Wubb Development