Disabling Buttons?

R

Ralph Freshour

How can I disable a button once it has been clicked? I want to prevent
the user from clicking on it twice if they have a slow connection.

Thanks...
 
G

Grant Wagner

Ralph said:
How can I disable a button once it has been clicked? I want to prevent
the user from clicking on it twice if they have a slow connection.

Thanks...

<input type="button" value="Please click me" class="button"
onclick="return singleClick(this, 'Please wait...');" />
<script type="text/javascript">
function singleClick(b, s) {

// move focus off the button
b.blur();

// has the button already been clicked?
if (typeof b.isClicked != "undefined" && b.isClicked) {
// yes, disallow the action
return false;
} else {
// no, flag that it has been clicked
b.isClicked = true;

// should we change the button text?
if (typeof s != "undefined") {

// yes, is there support to set the width of a button?
if (typeof b.style != "undefined" &&
typeof b.style.width != "undefined" &&
typeof b.offsetWidth != "undefined") {

// yes, set the width to the current width
b.style.width = b.offsetWidth + "px";
}

// change the text on the button
b.value = s;

// is there support to set the CSS class of a button?
if (typeof b.className != "undefined") {
// yes, set the CSS class to "buttonDisabled"
// (defined in css)
b.className = "buttonDisabled";
}
}

// allow the action
return true;
}
}
</script>

One important point with this code, the "Please wait..." text you pass to
the button *must* be shorter then the current text. Making it longer would
significantly complicate the procedure. Alternatively, you could dispose
of changing the text and the className entirely, since it's all just
cosmetic.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
@

@SM

Ralph Freshour a ecrit :
How can I disable a button once it has been clicked? I want to prevent
the user from clicking on it twice if they have a slow connection.

What kind of button ?

a radio or checkbox

<input type=radio onclick="if(this.checked) this.disabled;">

a button

<input type=button value="Go On"
onclick="if(this.value=='Go On')
{function1(); function2(); this.value='Disabled';}
">
 
G

Grant Wagner

Chris said:
Or:

<html>

<body>
<form id="MyForm" ACTION="http://example.microsoft.com/sample.asp"
METHOD="POST">
<input type="submit" value="Press Me"
onclick="this.disabled=true;document.all.MyForm.submit();">
</input>
<form>
</body>

</html>

Regards,
Chris.

It's not properly formed HTML (there is no closing </input> tag and it should
be </form>, not <form>), but I'll assume those were typos (although I really
hope you don't write forms with opening and closing <input></input> tag sets).

As for the JavaScript on the page, well, in Mozilla Firebird (and other Gecko
based browsers), it generates the following error and fails to prevent a
second click on the button: Error: document.all has no properties

In Netscape 4.78 it generates the following error and fails to prevent a
second click on the button: document.all has no properties

It also fails to provide any sort of feedback mechanism to the user as to
*why* the button is no longer working (although that isn't as important as the
next point) and it will not submit the form at all if client-side JavaScript
is disabled.

So, before you unceremoniously ...snip... a working solution and replace it
with your own, perhaps you should ensure that your solution works as well in
as many browsers, does as much, and provides the desired functionality even if
the user chooses to disable client-side JavaScript.

There is more to JavaScript authoring then having the script work in one, or
even many, browsers. Part of being a good JavaScript programmer is ensuring
that whatever you are doing degrades gracefully and provides as much support
for as many people as possible. Even in an Intranet environment, this should
be the goal, because although your company runs Internet Explorer today, they
may not always run it, and having a solution that can be ported to other
browsers more readily will, ultimately, make your job easier.

Besides, providing the sort of "solution" you just provided means you've just
trained another JavaScript author to be sloppy, careless and care little for
the portion of their audience not using the tools you expect them to be using
(Internet Explorer with client-side JavaScript enabled).

My guess is it was getting lonely in the Sloppy, Careless and Care Little Club
and you wanted some new members.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
L

Lasse Reichstein Nielsen

@SM said:
Chris a ecrit :

beuark ! that's for IE ! ( and others what do they do ? )

onclick="this.disabled=true;this.form.submit();"

I'd do that in IE too.
/L
 
C

Chris

@SM said:
Chris a ecrit :

beuark ! that's for IE ! ( and others what do they do ? )

Stephane,

I ran a very simple test of this on IE 6.0 and NN7.0 (so I assume Mozilla
will work as well).
In what respect do you think this may cause a problem.

Regards,
Chris.
p.s. beuark is spelt burk in English.
You can also be abusive in French if you wish as I speak that too.
In fact you can be abusive in Russian if you like.
I don't speak Russian, but it will make no difference.
 
I

Ivan Marsh

beuark ! that's for IE ! ( and others what do they do ? )

Stephane,

I ran a very simple test of this on IE 6.0 and NN7.0 (so I assume
Mozilla will work as well).
In what respect do you think this may cause a problem.[/QUOTE]

I was having trouble with the old HTML button double-click on a PHP script
that does a very large query (in excess of five minutes to process).

I use the 'onclick="this.disabled=true;..." method for IE and Mozilla and
it works fine in both.
 
G

GIMME

You are better off having a hidden variable with the value of a timestamp.

Then look in the database and see if you've already created a record
for the transaction.
 
C

Chris

.... snip ...

Hi Grant,

I was not intending to replace your posting, which was already available to
the questioner.
He asked how to disable a button and I noticed you had failed to mention the
'disabled' property of the submit button.
I see you noticed a couple of typos in the HTML I put in as a working
example.
I'm sure these corrections will be useful to the questioner should he decide
to make use of the suggestions I offered ;-)

Regards,
Chris.
 
M

Michael Winter

Chris wrote on 17 Dec 2003 at Wed, 17 Dec 2003 22:22:32 GMT:
In what respect do you think this may cause a problem.

The collection, document.all, is a proprietary Microsoft feature.
Not all browsers support it. For those that don't, all that code
above will do is prevent the user from *ever* submitting the form
unless they discover that they can disable JavaScript.

Mike
 
L

Lasse Reichstein Nielsen

I ran a very simple test of this on IE 6.0 and NN7.0 (so I assume Mozilla
will work as well).
In what respect do you think this may cause a problem.

In that "document.all" is undefined in Mozilla/Netscape. That means
that the above gives rise to a Javascript error.

It might work anyway, but that is just because the form's normal
behavior takes over (you clicked the submit button, so the form
is submitted).

I would say that it's living on the edge to disable the submit button
between it being clicked and the form being submitted. It works, but
it could just as well have not worked. I would not depend on it.

/L
 
@

@SM

Chris a ecrit :
onclick="this.disabled=true; this.submit();"
onclick="this.disabled=true; document.forms['MyForm'].submit();"
onclick="this.disabled=true; document.forms[0].submit();"

Stephane,

I ran a very simple test of this on IE 6.0 and NN7.0 (so I assume Mozilla
will work as well).

*document.all* is IE language

it is possible some others browsers could speak fiew IE idioms

to be sure, it's preferable to use universal language (Ecma ?)
In what respect do you think this may cause a problem.

*document.all* will give an error on my favorite browser for instance
Regards,
Chris.
p.s. beuark is spelt burk in English.

perhaps
but I did mean that I said (in french) beeeeeaaarrrrrck
It doesn't appear to me that burk (in english) would sound the same.
Could you spel this sound in english to me ?
You can also be abusive in French if you wish as I speak that too.
So could you give me a translation (this above sentance)?
My english is not too goog.
In fact you can be abusive in Russian if you like.
I don't speak Russian, but it will make no difference.

Regards
Stéphane
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
p.s. beuark is spelt burk in English.

The OP evidently intended to represent a vulgar noise, descriptive of
the code shown by Ralph, and not a real English word.

But please do not make spelling corrections in a language that you
yourself cannot spell properly.

The proper English spellings are berk and burke, and not burk; the
former is a simple insult of vulgar derivation, and the second means,
roughly, cheat, being derived from a proper name.


Page js-date5.htm has lost its final "eval".
 
J

Jim Ley

Chris a ecrit :
onclick="this.disabled=true; this.submit();"
onclick="this.disabled=true; document.forms['MyForm'].submit();"
onclick="this.disabled=true; document.forms[0].submit();"

No reason to call submit at all, in fact it may be bad unless you also
cancel the default behaviour of the submit button, when you click a
submit button the form is going to be submitted unless you cancel the
event.

Also the this.disabled approach may result in your input submit
elements name/value pair not being sent with the submission.

this.disabled=true is all you need surely?
to be sure, it's preferable to use universal language (Ecma ?)

Generally it's preferable to use what works in the widest possible
situations with the lowest possible risk it shouldn't matter who
convened the meeting that chose it, but yeah all your solutions were
better than the simple .all solution suggested.

Jim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top