• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables in JS

 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone tell how do i use static variables in javascript and its usage with an example .

Please helpout
Thnx
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by static you mean variables that don't change across function calls (like the static keyword in Java) then javascript doesn't have them. I did run across this with an attempt at emulating them.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it really depends on what you mean by "static variables". If you mean *class* variables there are ways to emulate them (JavaScript doesn't really have classes per se). What's your use case? In other words, what, specifically, are you trying to do?
 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnxs for the suggestions ...

static in the sense like our java static keyword ....
i need to keep track number of times a buttons is clicked and the same count is to be used in a function which is called onload()..
help me out
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you needing to track this across users? Sessions? If the answer is yes, you'll need to keep track of it on the server.
 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no actually its nt for users.... it just for a session n once i log out the counter needs to be zeroed
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set a counter variable equal to 0 then on the button click do something like counter++. Is there something I'm missing?
 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did it but ..have declared a global variable n incremented on the click.. but once i access the same in the other function on load i get the value 0 so i needed a static thing...
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ms raaghu wrote:i did it but ..have declared a global variable n incremented on the click.. but once i access the same in the other function on load i get the value 0 so i needed a static thing...


Do you need to persist this counter across multiple pages? If yes, you'll have to do it on the server.
 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no ..not on multiple pages same page but i increment a global var initialized to 0 on click and am trying to access the same in another function which is called on load so ..my doubt is it taking the initialized value and nt the incremented one.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going to have to show some code now. I'm not really following what the problem is. Can you show what is happening on load as well as on click?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Than have a variable that has the default value and have another one that has the incremented value. Seems pretty straightforward.

Eric
 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
say i have onclick method


but wen onload method is called on save den the value of counter is 0 but i want actual count value
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The counter value is reset when the page is refreshed, navigated too, etc. It should always be zero when the page loads. If you read it before the click it should have a value. With your code above, I can not tell what your problem is.

Eric
 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup what you are telling is true it is getting resetted but i don wanna value to be reseted.. please help me out to get the actual value..i.e. the number of times the button clicked.. on page load also
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You either need to post the number back to the server and have the server set the JS variable, or you have to use cookies. There is no way to maintain variable state between pages.

Eric
 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric , its not between the pages its on the same page ...
and if at all its on multiple pages how to do
"
You either need to post the number back to the server and have the server set the JS variable,
"? could you please give me an example.

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the page refreshes, you submit a form, etc. It is like erasing a whiteboard and starting over.

How is your page loosing the value? What action is being taken place?

If it is a form submission:
1) Use a hidden field,
2) Set the hidden field's value with the value from the counter variable
3) Read it on the form submission
4) Write the value back to the page.

If it is a user clicking a link
1) Read and Set a Cookie [Plenty of information out on the net on how to set a cookie and read a cookie]

Eric
 
ms raaghu
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Eric... did it taking a hidden field and sending the same to the server and fetching back from the same ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic