Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static variables in Javasript

Status
Not open for further replies.

dpdg

Programmer
May 23, 2005
148
US
The word static means something different depending on what environment you are in, but in this case I refer to static as in a local variable that retains its value between function calls (inside a particular function). I know this exists in VB, but in Javascript I haven't yet figured out how to implement it.

I know, It's a rare thing that you would use something like this, but I have come into a situation that I need to use a static variable (in the VB sense).

I have this calendar that when you display it it works as you would expect and it gets you the date to put in the textbox. The problem is if you need to go to the next month, it triggers a postback, since it is a part of that page (it is a server side calendar control). As you would expect, the page is reloaded and the calendar disappears.

If I could set a "flag" in a function that would keep its value each time that function was called, that would work. I have tried a global variable, but that won't work either, because I only want to reset the variable under certain conditions when the function is called.

It would work OK if the I started out with the calendar being displayed on the first load, then it doesn't go away with every postback. But I don't want it to display the first time the app is loaded. So what I need is some way to set a flag that doesn't get reset on every postback.
 
why dont you set a server-side session that gets set when you first loadd the page.

Then this session will just set the js variable everytime you postback
 
It matters not whether you use local or global variables client-side. After your page is reloaded, all the data they hold will be lost anyway.

If you need to retain data over a page refresh, either use cookies, or pass the data to the server, do the refresh, and pass the data back.

Hope this helps,
Dan




[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
What I ended up doing was, I created a hidden field and I set the flag from the server the first time the page was loaded. If it was a postback the value doesn't get set.

So that worked out perfect for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top