Disabling Buttons?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralph Freshour

    Disabling Buttons?

    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...

  • Grant Wagner

    #2
    Re: Disabling Buttons?

    Ralph Freshour wrote:
    [color=blue]
    > 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...[/color]

    <input type="button" value="Please click me" class="button"
    onclick="return singleClick(thi s, '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 <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Gain technical skills through documentation and training, earn certifications and connect with the community


    * 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-deve...upgrade_2.html


    Comment

    • Chris

      #3
      Re: Disabling Buttons?


      "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
      news:3FE09ECC.2 A64527@agricore united.com...[color=blue]
      > Ralph Freshour wrote:
      >[color=green]
      > > 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...[/color]
      >
      > <input type="button" value="Please click me" class="button">
      > ... snip ...[/color]

      Or:

      <html>

      <body>
      <form id="MyForm" ACTION="http://example.microso ft.com/sample.asp"
      METHOD="POST">
      <input type="submit" value="Press Me"
      onclick="this.d isabled=true;do cument.all.MyFo rm.submit();">
      </input>
      <form>
      </body>

      </html>

      Regards,
      Chris.


      Comment

      • @SM

        #4
        Re: Disabling Buttons?

        Chris a ecrit :[color=blue]
        >
        > "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
        > news:3FE09ECC.2 A64527@agricore united.com...[color=green]
        > > Ralph Freshour wrote:
        > >[/color]
        > onclick="this.d isabled=true;do cument.all.MyFo rm.submit();">[/color]

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

        Comment

        • @SM

          #5
          Re: Disabling Buttons?

          Ralph Freshour a ecrit :[color=blue]
          >
          > 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.[/color]

          What kind of button ?

          a radio or checkbox

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

          a button

          <input type=button value="Go On"
          onclick="if(thi s.value=='Go On')
          {function1(); function2(); this.value='Dis abled';}
          ">

          Comment

          • Grant Wagner

            #6
            Re: Disabling Buttons?

            Chris wrote:
            [color=blue]
            > "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
            > news:3FE09ECC.2 A64527@agricore united.com...[color=green]
            > > Ralph Freshour wrote:
            > >[color=darkred]
            > > > 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...[/color]
            > >
            > > <input type="button" value="Please click me" class="button">
            > > ... snip ...[/color]
            >
            > Or:
            >
            > <html>
            >
            > <body>
            > <form id="MyForm" ACTION="http://example.microso ft.com/sample.asp"
            > METHOD="POST">
            > <input type="submit" value="Press Me"
            > onclick="this.d isabled=true;do cument.all.MyFo rm.submit();">
            > </input>
            > <form>
            > </body>
            >
            > </html>
            >
            > Regards,
            > Chris.[/color]

            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 <gwagner@agrico reunited.com>

            * Client-side Javascript and Netscape 4 DOM Reference available at:
            *


            * Internet Explorer DOM Reference available at:
            *
            Gain technical skills through documentation and training, earn certifications and connect with the community


            * 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-deve...upgrade_2.html


            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: Disabling Buttons?

              "@SM" <stephane_moria ux@wanadoo.fr> writes:
              [color=blue]
              > Chris a ecrit :[color=green]
              >>
              >> "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
              >> news:3FE09ECC.2 A64527@agricore united.com...[color=darkred]
              >> > Ralph Freshour wrote:
              >> >[/color]
              >> onclick="this.d isabled=true;do cument.all.MyFo rm.submit();">[/color]
              >
              > beuark ! that's for IE ! ( and others what do they do ? )[/color]

              onclick="this.d isabled=true;th is.form.submit( );"

              I'd do that in IE too.
              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Chris

                #8
                Re: Disabling Buttons?


                "@SM" <stephane_moria ux@wanadoo.fr> wrote in message
                news:3FE0BF65.3 E570E5C@wanadoo .fr...[color=blue]
                > Chris a ecrit :[color=green]
                > >
                > > "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
                > > news:3FE09ECC.2 A64527@agricore united.com...[color=darkred]
                > > > Ralph Freshour wrote:
                > > >[/color]
                > > onclick="this.d isabled=true;do cument.all.MyFo rm.submit();">[/color]
                >
                > beuark ! that's for IE ! ( and others what do they do ? )[/color]

                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.


                Comment

                • Ivan Marsh

                  #9
                  Re: Disabling Buttons?

                  On Wed, 17 Dec 2003 22:22:32 +0000, Chris wrote:

                  [color=blue]
                  > "@SM" <stephane_moria ux@wanadoo.fr> wrote in message[/color]
                  <snip>[color=blue][color=green][color=darkred]
                  >> > > Ralph Freshour wrote:
                  >> > >
                  >> > onclick="this.d isabled=true;do cument.all.MyFo rm.submit();">[/color]
                  >>
                  >> beuark ! that's for IE ! ( and others what do they do ? )[/color]
                  >
                  > 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.[/color]

                  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.

                  --
                  i.m.
                  The USA Patriot Act is the most unpatriotic act in American history.

                  Comment

                  • Fabian

                    #10
                    Re: Disabling Buttons?

                    Chris hu kiteb:

                    [color=blue]
                    > Regards,
                    > Chris.
                    > p.s. beuark is spelt burk in English.[/color]

                    I love Skitt's Law.

                    Berk.

                    --
                    --
                    Fabian
                    Visit my website often and for long periods!


                    Comment

                    • GIMME

                      #11
                      Re: Disabling Buttons?

                      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.

                      Ralph Freshour <ralph@primemai l.com> wrote in message news:<ft11uvk71 p73pr7bsh3297cj dud5m8kmah@4ax. com>...[color=blue]
                      > 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...[/color]

                      Comment

                      • Chris

                        #12
                        Re: Disabling Buttons?


                        "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
                        news:3FE0C99B.3 34820DF@agricor eunited.com...[color=blue]
                        > Chris wrote:
                        >[color=green]
                        > > "Grant Wagner" <gwagner@agrico reunited.com> wrote[/color][/color]
                        .... 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.


                        Comment

                        • Lasse Reichstein Nielsen

                          #13
                          Re: Disabling Buttons?

                          "Chris" <c_bartlett@btc onnect.com> writes:
                          [color=blue][color=green][color=darkred]
                          >> > onclick="this.d isabled=true;do cument.all.MyFo rm.submit();">[/color][/color][/color]
                          [color=blue]
                          > 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.[/color]

                          In that "document.a ll" 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
                          --
                          Lasse Reichstein Nielsen - lrn@hotpop.com
                          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                          'Faith without judgement merely degrades the spirit divine.'

                          Comment

                          • @SM

                            #14
                            Re: Disabling Buttons?

                            Chris a ecrit :[color=blue]
                            >
                            > "@SM" <stephane_moria ux@wanadoo.fr> wrote in message
                            > news:3FE0BF65.3 E570E5C@wanadoo .fr...[color=green]
                            > > Chris a ecrit :[color=darkred]
                            > > >
                            > > > onclick="this.d isabled=true;do cument.all.MyFo rm.submit();">[/color]
                            > >
                            > > beuark ! that's for IE ! ( and others what do they do ? )[/color][/color]

                            onclick="this.d isabled=true; this.submit();"
                            onclick="this.d isabled=true; document.forms['MyForm'].submit();"
                            onclick="this.d isabled=true; document.forms[0].submit();"

                            [color=blue]
                            > Stephane,
                            >
                            > I ran a very simple test of this on IE 6.0 and NN7.0 (so I assume Mozilla
                            > will work as well).[/color]

                            *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 ?)
                            [color=blue]
                            > In what respect do you think this may cause a problem.[/color]

                            *document.all* will give an error on my favorite browser for instance
                            [color=blue]
                            > Regards,
                            > Chris.
                            > p.s. beuark is spelt burk in English.[/color]

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

                            Regards
                            Stéphane

                            Comment

                            • Michael Winter

                              #15
                              Re: Disabling Buttons?

                              Chris wrote on 17 Dec 2003 at Wed, 17 Dec 2003 22:22:32 GMT:
                              [color=blue]
                              > "@SM" <stephane_moria ux@wanadoo.fr> wrote in message
                              > news:3FE0BF65.3 E570E5C@wanadoo .fr...[color=green]
                              >> Chris a ecrit :[color=darkred]
                              >> >
                              >> > "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
                              >> > news:3FE09ECC.2 A64527@agricore united.com...
                              >> > > Ralph Freshour wrote:
                              >> > >
                              >> > onclick="this.d isabled=true;do cument.all.MyFo rm.submit();"[/color]
                              >>
                              >> beuark ! that's for IE ! ( and others what do they do ? )[/color]
                              >
                              > In what respect do you think this may cause a problem.[/color]

                              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

                              --
                              Michael Winter
                              M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

                              Comment

                              Working...