473,748 Members | 7,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>

Jul 23 '05 #1
13 1722
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()"><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
}

Jul 23 '05 #2
"RobB" <fe******@hotma il.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
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()"><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
}


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.
Jul 23 '05 #3
Tony wrote:
"RobB" <fe******@hotma il.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
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()"><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
}


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.


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.

Jul 23 '05 #4
RobB wrote:
Tony wrote:
RobB wrote: <snip>
<a href="#" onclick="return incValue()"> ...
<snip> 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.


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.


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.
Jul 23 '05 #5
lost hope wrote:
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>


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
Jul 23 '05 #6
Richard Cornford wrote:
RobB wrote:
Tony wrote:
RobB wrote: <snip> <a href="#" onclick="return incValue()"> ... <snip> 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.
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.


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

Jul 23 '05 #7
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!

Jul 23 '05 #8

"RobB" <fe******@hotma il.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Tony wrote:

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.


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.


I didn't get the impression that this was something that needed to be
spidered, but that is a good point to remember.
Jul 23 '05 #9
"RobB" <fe******@hotma il.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
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.
Didn't think about that :0
(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


Damn! I better start changing my code before they come breaking in my front
door to haul me off!
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2378
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any function at the top of the a file where you have them. //Tony
4
1219
by: Geoff Cox | last post by:
Hello, No doubt this is simple but I cannot see how to do it ... I have a variable called situation_number with a series of values 1, 2, 3 etc. I would like to have a series of variables situation-1, situation-2 etc where the numbers 1 and 2 come from the situation_number variable.
24
2526
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think that I am using them appropriate in class in question. One typical example: This class initiates TCP session, keeps sending commands to the server app and gets messages and codes back, message and code are stored in global variables to be preserved...
7
1353
by: Mark Prenter | last post by:
Hi all, I'm fairly new to .NET and I haven't done much in C++ before, nothing complex anyway, but I have a pretty good understanding of programming in general. What I'm trying to do is create a .DLL that contains a lot of the functions and classes that I normally use. I've followed the examples from http://www.c-sharpcorner.com/2/pr12.asp which contains a pretty good example of creating a simple .DLL. Even though the example is in C#,...
44
3638
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
37
2745
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
6
2036
by: Jim M | last post by:
I've been distributing a fairly mature, very specific MS Access application to end users in small offices of colleges for several years now. This is a part-time venture and low volume operation- this is somewhat of a hobby for me. Many of my end users are computer phobic and get little support from their IT departments. It is a split database so the datafile gets put on the file server and the 3 different front ends get put on each local...
10
3579
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring = "hello world";
26
1777
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.