How to maintain array values across the application
var Config = {
Windows: ['apple','mangi','lemon']
}
I push an item inside this array in page1, will the array maintain its state in the next screen if i call with the added new value. I want to keep an array or something which keep's on changing in every screen with its initial set of values.
var Config = {
Windows: ['banana','apple','mangi','lem开发者_Python百科on']
}
It's a bad practice to maintain state in a javascript variable across multiple pages. You should be relying on server side for that.
Having said that, if you are bent on doing this, consider storing the variable as a cookie and retrieving it after every page load.
Although I agree with the above posters, you could pass your variables as URL parameters.
This tutorial should be of some assitance: - http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
As you already have an 'array', you could parse the parameters as a JSON object.
e.g. http://happyc.at/page1.html?json={"Windows":["banana","apple","mango","lemon"]}
...just remember to parse the string as JSON (you can use $.parseJSON in jQuery).
Hope this helps.
Scripts on one page won't be available or remembered on another page without some sort of session-based help on your part.
I would use AJAX to do this, by:
- Storing your JavaScript array in PHP or some other server-side scripting language that supports sessions.
- Using AJAX to retrieve the list of items when you need it.
- Using AJAX to append an item to the list.
If you've never dealt with AJAX before, I highly recommend using the jQuery Javascript library. It takes all the hassle out of it for you.
精彩评论