globlas shmiglobas??? BIZARRE!!!

J

Juan Garcia

I have a file acGlobals.js with only one declaration

acCount = 0;

I have a html file where I include acGlobals.js and use that
variable... ie.

acCount++;
alert(acCount);

Problem I see is that everytime I refresh the page alert displays a
"1". ALWAYS. So I figured, the global was being reinitialized to 0
when I reload the page.... fair enough. So i changed the declaration
in acGlobals.js to not initialize to 0. Just

acCount;

Then alert displays "NaN." What gives? I then repeated the
experiment but added the "var" to the declaration... Same result.
What kind of global is this?

Any ideas what is going on? What am I doing wrong?

jg
 
L

Lasse Reichstein Nielsen

Problem I see is that everytime I refresh the page alert displays a
"1". ALWAYS. So I figured, the global was being reinitialized to 0
when I reload the page....

In fact, it is recreated.
fair enough. So i changed the declaration
in acGlobals.js to not initialize to 0. Just

acCount;

That is not a variable decalaration, it is just an attempt to read
a non-existant variable.
Then alert displays "NaN." What gives?

You have an uninitialized or undeclared variable, and you try to
increment it. Incrementing "undefined" gives "NaN", so that is what
you get.

I then repeated the
experiment but added the "var" to the declaration... Same result.
What kind of global is this?

It is an undeclared, or uninitialze "global". Also, in a browser there
is no *really* global variables. Each window or frame has its own
variables and none of them survives changing the content of the window.
In order to save information between pages, you should either open a
third window and store the information there, or you should use cookies.

/L
 
R

Richard Cornford

In order to save information between pages, you should either
open a third window and store the information there, or you
should use cookies.

Or send (post or get with a form) it to a server and have the server
include it in the next page, or add it as a query string on the URL of
the next page and read it from location.search when the page loads.

(and the third window could be an additional frame or framset page.)

Richard.
 
G

Grant Wagner

Juan said:
I have a file acGlobals.js with only one declaration

acCount = 0;

I have a html file where I include acGlobals.js and use that
variable... ie.

acCount++;
alert(acCount);

Problem I see is that everytime I refresh the page alert displays a
"1". ALWAYS. So I figured, the global was being reinitialized to 0
when I reload the page.... fair enough. So i changed the declaration
in acGlobals.js to not initialize to 0. Just

acCount;

Then alert displays "NaN." What gives? I then repeated the
experiment but added the "var" to the declaration... Same result.
What kind of global is this?

Any ideas what is going on? What am I doing wrong?

jg

Client-side JavaScript can not retain state. When you re-load the page,
all variables are set to their initial values. In the case of doing
something like:

var myNumber;
myNumber++;

myNumber was null prior to the attempt to increment it. null++ == NaN.

If you want to retain the value of the counter across subsequent page
loads, you'll need to store it's value in a cookie. Depending on how the
page is being reloaded (as part of a form submission?) you could store
the value in a hidden input, use method="GET" and retrieve it on the next
page.

--
| 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 6/7 and Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

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
474,077
Messages
2,570,569
Members
47,206
Latest member
MalorieSte

Latest Threads

Top