simple global variables question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lost hope

    #1

    simple global variables question

    hi, i have a very simple html page with a global variable, a function
    to increment its value, and a form that calls the function. however,
    what happens is that every time i click the image, the page is
    reloaded, and the value is reset to 1. is this what i should be
    expecting? are global variables not persistent across reloads? if
    this is the case, how can i set it up so that clicking the button
    doesn't reload the page automatically, but rather just calls the
    function. this is so frusterating :(


    <html>
    <head>
    <script language="javas cript">

    var a=1;

    function incValue()
    {
    a = a+5;
    }

    </script>
    </head>

    <body>

    <form>
    <input type="image" src="one.gif" onclick="incVal ue()"/>
    </form>


    </body>
    </html>

  • RobB

    #2
    Re: simple global variables question

    lost hope wrote:

    (snip)
    [color=blue]
    > ...are global variables not persistent across reloads?[/color]

    Nope. http is stateless, reload or not.
    [color=blue]
    > if this is the case, how can i set it up so that clicking the button
    > doesn't reload the page automatically, but rather just calls the
    > function. this is so frusterating :([/color]

    (snip)

    <input type="image"> is a graphical submit button. To avoid form
    submission and the loading of a new document, try:

    <a href="#" onclick="return incValue()"><im g src="one.gif" /></a>

    var a = 1;
    //watch that title bar !
    document.title = 'a = ' + a;
    function incValue()
    {
    a += 5;
    document.title = 'a = ' + a;
    return false; //kill link default
    }

    Comment

    • Tony

      #3
      Re: simple global variables question

      "RobB" <ferndoc9@hotma il.com> wrote in message
      news:1114662210 .940152.121150@ l41g2000cwc.goo glegroups.com.. .[color=blue]
      > lost hope wrote:
      >
      > (snip)
      >[color=green]
      >> ...are global variables not persistent across reloads?[/color]
      >
      > Nope. http is stateless, reload or not.
      >[color=green]
      >> if this is the case, how can i set it up so that clicking the button
      >> doesn't reload the page automatically, but rather just calls the
      >> function. this is so frusterating :([/color]
      >
      > (snip)
      >
      > <input type="image"> is a graphical submit button. To avoid form
      > submission and the loading of a new document, try:
      >
      > <a href="#" onclick="return incValue()"><im g src="one.gif" /></a>
      >
      > var a = 1;
      > //watch that title bar !
      > document.title = 'a = ' + a;
      > function incValue()
      > {
      > a += 5;
      > document.title = 'a = ' + a;
      > return false; //kill link default
      > }[/color]

      For me, just using <img src="image.gif" onclick="incVal ue()"> has worked.
      The image isn't a link or form input, but it still responds to "onclick". I
      don't know if it works in browsers other than IE, though.


      Comment

      • RobB

        #4
        Re: simple global variables question

        Tony wrote:[color=blue]
        > "RobB" <ferndoc9@hotma il.com> wrote in message
        > news:1114662210 .940152.121150@ l41g2000cwc.goo glegroups.com.. .[color=green]
        > > lost hope wrote:
        > >
        > > (snip)
        > >[color=darkred]
        > >> ...are global variables not persistent across reloads?[/color]
        > >
        > > Nope. http is stateless, reload or not.
        > >[color=darkred]
        > >> if this is the case, how can i set it up so that clicking the[/color][/color][/color]
        button[color=blue][color=green][color=darkred]
        > >> doesn't reload the page automatically, but rather just calls the
        > >> function. this is so frusterating :([/color]
        > >
        > > (snip)
        > >
        > > <input type="image"> is a graphical submit button. To avoid form
        > > submission and the loading of a new document, try:
        > >
        > > <a href="#" onclick="return incValue()"><im g src="one.gif" /></a>
        > >
        > > var a = 1;
        > > //watch that title bar !
        > > document.title = 'a = ' + a;
        > > function incValue()
        > > {
        > > a += 5;
        > > document.title = 'a = ' + a;
        > > return false; //kill link default
        > > }[/color]
        >
        > For me, just using <img src="image.gif" onclick="incVal ue()"> has[/color]
        worked.[color=blue]
        > The image isn't a link or form input, but it still responds to[/color]
        "onclick". I[color=blue]
        > don't know if it works in browsers other than IE, though.[/color]

        That's ok, Image objects have onclick handlers too, in almost all
        current browsers. Probably not a good habit to get into, however, as
        they won't be spidered as links are. Add style="cursor:p ointer;" for a
        more link-like appearance.

        Comment

        • Richard Cornford

          #5
          Re: simple global variables question

          RobB wrote:[color=blue]
          > Tony wrote:[color=green]
          >> RobB wrote:[/color][/color]
          <snip>[color=blue][color=green][color=darkred]
          >>> <a href="#" onclick="return incValue()"> ...[/color][/color][/color]
          <snip>[color=blue][color=green]
          >> For me, just using <img src="image.gif" onclick="incVal ue()">
          >> has worked. The image isn't a link or form input, but it
          >> still responds to "onclick". I don't know if it works in
          >> browsers other than IE, though.[/color]
          >
          > That's ok, Image objects have onclick handlers too, in almost
          > all current browsers. Probably not a good habit to get into,
          > however, as they won't be spidered as links are. Add
          > style="cursor:p ointer;" for a more link-like appearance.[/color]

          It is difficult to see what benefits can follow from having href="#"
          spidered. What is lost when an onclick handler is used with an image is
          any potential built into the browser for the use of an input device that
          is not a 'pointing device' (such as a mouse). Denying the possibility of
          using a keyboard for input (besides inconveniencing those that use
          keyboards for input habitually) would mean that the result failed to be
          accessible, and so would be illegal in some contexts throughout the
          world (and antisocial in the rest). Although it would be possible to
          implement the keyboard-based operation of the site within the script,
          but that is a lot of additional work just to reproduce what browsers can
          otherwise do for you (and even more complex to achieve reliably
          cross-browser).

          Richard.


          Comment

          • RobG

            #6
            Re: simple global variables question

            lost hope wrote:[color=blue]
            > hi, i have a very simple html page with a global variable, a function
            > to increment its value, and a form that calls the function. however,
            > what happens is that every time i click the image, the page is
            > reloaded, and the value is reset to 1. is this what i should be
            > expecting? are global variables not persistent across reloads? if
            > this is the case, how can i set it up so that clicking the button
            > doesn't reload the page automatically, but rather just calls the
            > function. this is so frusterating :(
            >
            >
            > <html>
            > <head>
            > <script language="javas cript">
            >
            > var a=1;
            >
            > function incValue()
            > {
            > a = a+5;
            > }
            >
            > </script>
            > </head>
            >
            > <body>
            >
            > <form>
            > <input type="image" src="one.gif" onclick="incVal ue()"/>
            > </form>
            >
            >
            > </body>
            > </html>
            >[/color]

            If all you are using it for is to create a clickable image, just have
            the input on its own with no form - then there's no form to submit
            and no href to follow.

            Users without JavaScript may wonder what the image is supposed to do
            since they will see a pointer cursor when hovering over it, but
            nothing will happen when they click.


            --
            Rob

            Comment

            • RobB

              #7
              Re: simple global variables question

              Richard Cornford wrote:[color=blue]
              > RobB wrote:[color=green]
              > > Tony wrote:[color=darkred]
              > >> RobB wrote:[/color][/color]
              > <snip>[color=green][color=darkred]
              > >>> <a href="#" onclick="return incValue()"> ...[/color][/color]
              > <snip>[color=green][color=darkred]
              > >> For me, just using <img src="image.gif" onclick="incVal ue()">
              > >> has worked. The image isn't a link or form input, but it
              > >> still responds to "onclick". I don't know if it works in
              > >> browsers other than IE, though.[/color]
              > >
              > > That's ok, Image objects have onclick handlers too, in almost
              > > all current browsers. Probably not a good habit to get into,
              > > however, as they won't be spidered as links are. Add
              > > style="cursor:p ointer;" for a more link-like appearance.[/color]
              >
              > It is difficult to see what benefits can follow from having href="#"
              > spidered.[/color]

              Hmm...didn't I say -

              "Probably not a good habit to get into..."

              ....signifying that it was the *practice* of using image onclicks that
              was being commented on, not the (trivial) example at hand. Certainly
              agree about input devices; as well, all possibility of a default
              alternative for JS-disabled users is removed - again, meaningless for
              the above but worthy of thought as a general rule.

              (snip)
              [color=blue]
              > Denying the possibility of using a keyboard for input...
              > would be illegal in some contexts throughout the world [ .. ][/color]

              Are there people doing time for Image.onclick? #:=0

              Comment

              • lost hope

                #8
                Re: simple global variables question

                hey guys,
                thanks for all your help -- everything is much clearer now =) i
                figured global state wasn't persistent across page reloads, but i
                couldn't see why clicking the input (of type image) was causing the
                refresh. now i understand. yay!

                Comment

                • Tony

                  #9
                  Re: simple global variables question


                  "RobB" <ferndoc9@hotma il.com> wrote in message
                  news:1114666300 .954214.287250@ g14g2000cwa.goo glegroups.com.. .[color=blue]
                  > Tony wrote:[color=green]
                  >>
                  >> For me, just using <img src="image.gif" onclick="incVal ue()"> has[/color]
                  > worked.[color=green]
                  >> The image isn't a link or form input, but it still responds to[/color]
                  > "onclick". I[color=green]
                  >> don't know if it works in browsers other than IE, though.[/color]
                  >
                  > That's ok, Image objects have onclick handlers too, in almost all
                  > current browsers. Probably not a good habit to get into, however, as
                  > they won't be spidered as links are. Add style="cursor:p ointer;" for a
                  > more link-like appearance.[/color]

                  I didn't get the impression that this was something that needed to be
                  spidered, but that is a good point to remember.


                  Comment

                  • Tony

                    #10
                    Re: simple global variables question

                    "RobB" <ferndoc9@hotma il.com> wrote in message
                    news:1114676042 .169175.294170@ g14g2000cwa.goo glegroups.com.. .[color=blue]
                    > Richard Cornford wrote:
                    >
                    > "Probably not a good habit to get into..."
                    >
                    > ...signifying that it was the *practice* of using image onclicks that
                    > was being commented on, not the (trivial) example at hand. Certainly
                    > agree about input devices; as well, all possibility of a default
                    > alternative for JS-disabled users is removed - again, meaningless for
                    > the above but worthy of thought as a general rule.[/color]

                    Didn't think about that :0
                    [color=blue]
                    > (snip)
                    >[color=green]
                    >> Denying the possibility of using a keyboard for input...
                    >> would be illegal in some contexts throughout the world [ .. ][/color]
                    >
                    > Are there people doing time for Image.onclick? #:=0[/color]

                    Damn! I better start changing my code before they come breaking in my front
                    door to haul me off!


                    Comment

                    • Dr John Stockton

                      #11
                      Re: simple global variables question

                      JRS: In article <d4pu1m$dj$1$83 00dec7@news.dem on.co.uk>, dated Thu, 28
                      Apr 2005 06:58:13, seen in news:comp.lang. javascript, Richard Cornford
                      <Richard@litote s.demon.co.uk> posted :
                      [color=blue]
                      > What is lost when an onclick handler is used with an image is
                      >any potential built into the browser for the use of an input device that
                      >is not a 'pointing device' (such as a mouse). Denying the possibility of
                      >using a keyboard for input (besides inconveniencing those that use
                      >keyboards for input habitually) would mean that the result failed to be
                      >accessible, and so would be illegal in some contexts throughout the
                      >world (and antisocial in the rest). Although it would be possible to
                      >implement the keyboard-based operation of the site within the script,
                      >but that is a lot of additional work just to reproduce what browsers can
                      >otherwise do for you (and even more complex to achieve reliably
                      >cross-browser).[/color]

                      Not necessarily, as far as I can see.

                      If the "mouse" is the only way of getting the effect that onClick
                      provides, then indeed those without a "mouse" may be improperly
                      handicapped.

                      But if the site is designed so that the onClick effect can be obtained
                      independently of the image and without a "mouse", then ISTM perfectly
                      reasonable to add another way to get the effect, using "mouse" and
                      onClick, to the advantage of those who can use it.

                      And the clickable image may be a positive advantage for those who can
                      use a pointing device but not a keyboard.


                      After all, although our Public Library does stock large-print books, it
                      also stocks rather more ordinary books which not everybody can read.

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                      Comment

                      • Richard Cornford

                        #12
                        Re: simple global variables question

                        Dr John Stockton wrote:[color=blue]
                        > Richard Cornford posted :
                        >[color=green]
                        >> What is lost when an onclick handler is used with an
                        >> image is any potential built into the browser for the
                        >> use of an input device that is not a 'pointing device'
                        >> (such as a mouse). ...[/color][/color]
                        <snip>[color=blue]
                        > Not necessarily, as far as I can see.
                        >
                        > If the "mouse" is the only way of getting the effect
                        > that onClick provides, then indeed those without a
                        > "mouse" may be improperly handicapped.[/color]

                        Whether a mouse is the only means of activating an onclick handler
                        depends a great deal on the HTML element on which the handler is set,
                        and a little on the browser that is being used. With the various
                        button-type elements, and A elements (or, pretty much any element that
                        can be navigated to with the tab key on the keyboard) the bulk of modern
                        browsers will fire the onclick event when the 'activating' action is
                        performed with the keyboard (be that hitting the return key, or
                        whatever).

                        The problem with an IMG element is that it is not a natural receiver of
                        focus, and so cannot be targeted by keyboard input events. Leaving the
                        programmer with the problem of intercepting keyboard input at a higher
                        level (say the document) and working out when, and what, input from the
                        keyboard should relate to the IMG. While warping the IMG in A, or using
                        a button-type element would allow the browser to do the work instead.
                        [color=blue]
                        > But if the site is designed so that the onClick effect can
                        > be obtained independently of the image and without a "mouse",
                        > then ISTM perfectly reasonable to add another way to get the
                        > effect, using "mouse" and onClick, to the advantage of those
                        > who can use it.
                        >
                        > And the clickable image may be a positive advantage for those
                        > who can use a pointing device but not a keyboard.[/color]

                        Yes, accessibility requires that the operation of the page be
                        independent of any particular input device, and you certainly wouldn't
                        achieve that by excluding the use of a mouse.
                        [color=blue]
                        > After all, although our Public Library does stock large-print
                        > books, it also stocks rather more ordinary books which not
                        > everybody can read.[/color]

                        I point out the possibility of alternative input devices because it is
                        common for individuals who use a particular method themselves to assume
                        that everybody else is just like them. An attitude that is manifest in
                        the common use of the phrase "click here". Accessibility is an area
                        where that attitude is particularly problematic, but in reality
                        diversity the behaviour of user's with regard to preferred input devices
                        is already considerable without even considering those who may have
                        special requirements.

                        Richard.


                        Comment

                        • Richard Cornford

                          #13
                          Re: simple global variables question

                          RobB wrote:[color=blue]
                          > Richard Cornford wrote:[/color]
                          <snip>[color=blue][color=green]
                          >> Denying the possibility of using a keyboard for input...
                          >> would be illegal in some contexts throughout the world [ .. ][/color]
                          >
                          > Are there people doing time for Image.onclick? #:=0[/color]

                          All of the legislation that I am aware of uses financial penalties
                          rather than imprisonment (though those not paying their fines,
                          court-awarded damages, compensation, costs, etc, may become subject to
                          eventual imprisonment).

                          Richard.


                          Comment

                          • Dr John Stockton

                            #14
                            Re: simple global variables question

                            JRS: In article <d50ssn$5u6$1$8 302bc10@news.de mon.co.uk>, dated Sat, 30
                            Apr 2005 22:21:26, seen in news:comp.lang. javascript, Richard Cornford
                            <Richard@litote s.demon.co.uk> posted :[color=blue]
                            >Dr John Stockton wrote:[color=green]
                            >> Richard Cornford posted :
                            >>[color=darkred]
                            >>> What is lost when an onclick handler is used with an
                            >>> image is any potential built into the browser for the
                            >>> use of an input device that is not a 'pointing device'
                            >>> (such as a mouse). ...[/color][/color]
                            ><snip>[color=green]
                            >> Not necessarily, as far as I can see.
                            >>
                            >> If the "mouse" is the only way of getting the effect
                            >> that onClick provides, then indeed those without a
                            >> "mouse" may be improperly handicapped.[/color]
                            >
                            >Whether a mouse is the only means of activating an onclick handler
                            >depends a great deal on the HTML element on which the handler is set,
                            >and a little on the browser that is being used. With the various
                            >button-type elements, and A elements (or, pretty much any element that
                            >can be navigated to with the tab key on the keyboard) the bulk of modern
                            >browsers will fire the onclick event when the 'activating' action is
                            >performed with the keyboard (be that hitting the return key, or
                            >whatever).[/color]

                            I wrote not "an onclick handler" but "the effect that onClick provides".

                            In some cases, one might provide a Button which calls the same function
                            as the image onClick.

                            If the onClick is used to supply X & Y co-ordinates, that might
                            otherwise be done by "<input type=text ...>".

                            There is, fortunately, no legal requirement to put all users on the same
                            footing by disabling the able; the requirement is that the less able
                            should not be unnecessarily disadvantaged.

                            --
                            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                            Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
                            Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
                            Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

                            Comment

                            Working...