Best Practice for Storing Many Client-Side Javascript Static Variables - javascript

I have an application in which everything is accomplished, preferably, on the client side. JavaScript on the client side will determine the country location of the user using geolocation. Afterwards, however, the client application needs to perform calculations based upon what country the user is from. I have a list of variables for each country - UK:176 - for example - how should I deliver these variables to the client?
One thing crossing my mind was to store the data in hidden html tables, that the JavaScript could fetch based upon country later. However, that solution is very hacky. And obviously having some 200+ JS variables would harm performance. Is local web storage a good alternative, for example?

If you have to store the variables on the client side, yes, then local web storage is probably the best way to go. If you don't need the data persistent you could also store the variables in the session storage. As the name implies, this store is lost if the user closes the session (closing the tab or browser).

Related

What's the need memcached with node.js?

I am developing a simple login system, and I'll have to store two cookies. One indicating which user is logged, and other indicating the "plan" (database) that the user belongs. Well, at client side the first cookie (us_auth) and the second (pl_auth), will store hashes (md5) to make a key that I can check on server side.
Now at the server side I'll store these two keys to assign to them values to route the user correctly. A simple authentication system.
Well, here the question comes. At PHP we have $_SESSION, and at Node we have modules that make the same thing, but I want something that I have more control, to work directly with the data. I have two choices:
Memcached, or a simple global var that store an object with key->value (in my case, hash->value).
What's the best choice in my case? At simple point of view, the second alternative seems to be more fast, pratical and simple.
A simple global variable will fail if you replicate your application. Variables are not shared between processes (or even machines).
If you don’t plan on replicating your app, just restart your process, the memory allocated to that process will be lost, so will your in-memory session variables.
A memcached server can help you with that.
This is not specific to nodejs. You’ll have the same problem with PHP or any other technology.

When should I use PHP Session vs Browser Local Storage vs JavaScript Object Parameters?

When is it appropriate to use the many different ways that modern day AJAX based applications are storing data? I'm hoping for some specific guidelines that I can give developers. Here's what I'm seeing so far, and it's getting messy.
PHP Server Side Session: PHP Session data is probably the oldest way to store session based information. I'm often passing in parameters through various AJAX calls from JavaScript/jQuery objects - to store in PHP Session. I'm also returning objects of data (some session information) back through as a response/result to JavaScript/jQuery methods.
Browser based Local Storage: This is often used to store data that needs to persist on the front end, yet I'm uncertain at times when to use it. One good use was to store geolocation information from navigator.geolocation. I've been storing a lot of information here, but I am not certain that is wise. It never seems to expire, but can be deleted from Resources.
JavaScript Object with config parameter(s): I've been building JavaScipts objects with an init method that sets-up a 'settings' parameter. This is very useful as I usually build it from data passed in from PHP. With jQuery Mobile this data can even persist from page to page and change with AJAX request responses.
So, what guidelines would you give on usage of each?
PHP Session data is NOT Permanent Data storage as when you destroy the browsers session you will loose the Data. This is useful if you dont
want to permanently store data.
Browsers Local Storage is Permanent unless you delete the data yourself or you clear the browsers cache. Some users clear the cache from time to time so this can be a problem.
Any other method Such as Objects is not permanent Data storage.
Other Browser related Permanent storage are COOKIES (if you don't
expire them at session close), IndexedDb (Check here for current browser support http://caniuse.com/#feat=indexeddb).
So depending on your Website or App you need to decide what data needs to be
stored for a short time, or for long time or forever until you deleted it manually.
As an example, you will use LocalStorage if you were storing
Bookmarks, and if you were Storing Geolocation points you use Cookies
and expire them after you close the browser or the App.
If you were Logging in to an Account using PHP then best practice is to create a PHP
Session, and even change the session timeout when the user clicks
(Remember me).
These are just a couple of examples from thousands of possible needs.

javascript pass a variable to another page

Ok, Here is what I want to do. I have a very complex database. I am reading from the database using php and store the data in a variable (Ajax). Now when I go to another page I loose the variable.
Here is what I know (not sure how correct I am): the scope of a variable is the function it is in. If not specified the scope will be the window object (which changes for every page). Is there any higher level than window? (something like session in php but in the client side)
There are many way to storage your variable such as:
Use client cookie
Use server session or cookie
HTML5 storage
Query string such as ?var1=blah&var2=blah
Maybe you can use cookie. Actually php sessions also utilize cookie as well. As far as I know the scope beyong window is cookie.
Or you can use some HTML5 technologies, such as WebStorage, but that may break on some old browsers.
Normally I would avoid storing state in javascript and instead cache the result on the server. Some sort of application or user level cache might be recommended in this case, so that the other page may benefit from the db result. If it's per user then I would look into session. Otherwise it might be better to store it at the application level in some sort of cache

Standard Way of Storing User Preferences on Client via Web Application

I realize this might not be a best fit for SO, so please tell me where I should move/post this if that is the case.
My idea is after a user signs into the system, store the user preferences on the client in the form of cookies. If a user modifies said preferences, update the cookies. I need to do this because some of the preferences are client related and will need to be looked at via JavaScript.
I realize I'll need the preferences stored on the server as well. Just wanting to know if pulling them down into cookies is a good idea. My app is primarily ajax driven so I'd like to pull the preferences down once and just store them. I don't want to push them with each server request.
I'd like to avoid things like Local Storage so that I don't have to worry about browsers as much. Cookies seem to be supported heavily by all browsers.
Anyone concur or have a better way?
EDIT now that you've changed the question from when we first wrote answers:
If you are storing the preference data on the server, then there is no reason to keep preferences data on the local user's computer between visits As such there is no reason to put the data in a cookie as it just increases the size of every client/server roundtrip and takes storage on the client computer necessarily.
Instead, I would suggest that you simply put a preferences object in the page's javascript like this:
var userPref = {theme: "fun", permitContact: false, lastDisplay: "threaded"};
Then, you can get access to the preference values via javascript from any page with code like this:
if (userPref.lastDisplay == "threaded") {
// do something
}
Old answer before the question was edited:
If you want the client preferences to work from different browsers that the client might use, then you should store the preferences on your server (highly recommended). You can make them available in the web page at any time, by just including a small amount of javascript in each page that sets the properties of a preferences object to the value of the user's preferences. And, then you can have a preferences page where the user can modify/update the preferences and store the newly changed prefs back on the server again.
Cookies are for temporal state that may get cleared at any time and will only ever work on that particular computer. Plus, if you use cookies and if userA logs out and userB logs in on the same computer, the preferences will be wrong for userB.
Generally cookies are a good idea, provided that you don't have to store a lot of data. Just few things to keep in mind when using cookies to store stuff:
user can edit the preferences by hand so make sure you don't have things like is_root=false without additional server side checks.
this can be annoying when user removes cookies and preferences are gone, I would go for a server side preferences storage mirrored to cookies so JavaScript can use them.
Other possibility would be to serve dynamic JavaScript, with inlined preferences, instead of static files - you could serve JavaScript the same as you serve dynamic HTML, but then you have to be careful with caching.
You'll need to store the preferences on the server either way, as you don't want to trouble your user with having to re-create their personal settings every time they sign on with a different computer.
Just store them serverside.

JavaScript - Storing data during user interaction

I'm working on a web based form builder that uses a mix of Jquery and PHP server side interaction. While the user is building the form I'm trying to determine the best method to store each of one of the form items before all the data is sent to the server. I've looked at the following methods
Javascript arrays
XML document
Send each form item to the server side to be stored in a session
The good, the bad and the ugly
Depends on your application functionality and requirements, but Javascript would probably be the best way. You can use either arrays or objects or whatever in javascript. It's server independent and it will preserve data over a long period of time as long as client session stays present (browser window doesn't close for whatever reason) but this can be quite easily avoided (check my last paragraph).
Using XML documents would be the worst solution because XML is not as well supported on the client side as you might think.
Server side sessions are good and bad. They are fine if you store intermediate results from time to time, so if client session ends because of whatever reason, user doesn't loose all data. But the problem is that it may as well expire on the server.
If I was you, I'd use Javascript storage and if needed occasionally send JSON serialized results to server and persist them there as well (based on business process storig this data somewhere else than session could be a better solution). I'd do the second part (with sever side combination) only if I would know that user will most probably build forms in multiple stages over a longer period of time and multiple client sessions. but can be used for failure preventions as well. Anyway. Javascript is your best bet with possible server-side interaction.
Preserving data between pages on the client
Be aware that it's also possible to preseve data between pages on the client side. Check sessvars library for this. So even if the page gets refreshed or redirected and then returned all this can be stored on the client side between these events like magic. Marvelous any rather tiny library that made my life several times. And lessened application complexity considerably that would otherwise have to be implemented with something more complex.
I used TaffyDB to store data, and it's just wonderfully easy to implement.
Hope this helps you
You may want to check out PersistJS, which exposes a cross-browser persistent storage object. Of course, being persistent, data stored with this library survives sessions, not just page changes.
The latest version (0.2.0) is here – note the version in the above linked post is 0.1.0.
A combination of #1 (although I'd use objects, not arrays necessarily) and #3 would seem like a good approach. Storing the data locally in the browser (#1) makes it immediately accessible. Backing that up with session-based server-side storage defends you from the page being refreshed; you can magically restore the page just as it was.

Categories

Resources