simple global variables question

L

lost hope

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="javascript">

var a=1;

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

</script>
</head>

<body>

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


</body>
</html>
 
R

RobB

lost hope wrote:

(snip)
...are global variables not persistent across reloads?

Nope. http is stateless, reload or not.
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 :(

(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()"><img 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
}
 
R

RobB

Tony said:
For me, just using <img src="image.gif" onclick="incValue()"> 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.

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:pointer;" for a
more link-like appearance.
 
R

Richard Cornford

RobB said:
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:pointer;" for a more link-like appearance.

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

RobG

lost said:
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="javascript">

var a=1;

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

</script>
</head>

<body>

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


</body>
</html>

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

RobB

Richard said:
It is difficult to see what benefits can follow from having href="#"
spidered.

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)
Denying the possibility of using a keyboard for input...
would be illegal in some contexts throughout the world [ .. ]

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

lost hope

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!
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 28
Apr 2005 06:58:13, seen in Richard Cornford
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).

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

Richard Cornford

Dr said:
Richard Cornford posted :
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.

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

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.
After all, although our Public Library does stock large-print
books, it also stocks rather more ordinary books which not
everybody can read.

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

Richard Cornford

RobB said:
Richard Cornford wrote:
Denying the possibility of using a keyboard for input...
would be illegal in some contexts throughout the world [ .. ]

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

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

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 30
Apr 2005 22:21:26, seen in Richard Cornford
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).

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.
 

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

Forum statistics

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

Latest Threads

Top