File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Static variables in JS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Static variables in JS" Watch "Static variables in JS" New topic
Author

Static variables in JS

ms raaghu
Ranch Hand

Joined: Jul 10, 2009
Posts: 66
Hi,
Can anyone tell how do i use static variables in javascript and its usage with an example .

Please helpout
Thnx
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 10, 2009
Posts: 66
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Jul 10, 2009
Posts: 66
no actually its nt for users.... it just for a session n once i log out the counter needs to be zeroed
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Jul 10, 2009
Posts: 66
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Jul 10, 2009
Posts: 66
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 10, 2009
Posts: 66
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 10, 2009
Posts: 66
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 10, 2009
Posts: 66
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 10, 2009
Posts: 66
Thanks a lot Eric... did it taking a hidden field and sending the same to the server and fetching back from the same ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static variables in JS