I would like to ask a question about a new and large scale web project’s javascript requirements. We will use lot of javascript, ajax requests, jquery, json objects & jquery plugins in our project. We planning to store global variables and lot of default values in a global site configuration file with php class and ini file on server-side.
But we need to read, use and sometimes override some variables and configuration values on client-side with javascript – jquery.
This javascript based configuration file must have following properties;
- Won’t have all server-side config values. Only we need.
- Must be a single file that will be call on html head section.
- Must define a global variable or json or javasctipt object or array (I don’t know which is best)
- This values must reachable by other javascript functions and objects.
- Will store booleans, strings, integers maybe a some little initialization methods for 5-6 different pages (ex.: we don’t need main page’s config values on product detail page’s and we don’t need product detail page’s some initialization methods and values on main page etc.)
- We need to reach some values of this configuration object on every page like debugMode=true or false etc..
- We need to know in other javascript objects to running platform via this config file for images and other resource paths (Developer-Test-Stage-Production)
Also we can completely generate this file on server side or generate a static .js file and after a PHP request, set some user-page-specific or language specific values, than we must be put (override) some of this server-side generated values in Js object.
What is best-practices for this solution? Any suggestions?
JSON is basically an object literal, so it can do both. Go for it. Think of JSON as a serialized javascript object.
As soon as you run the JSON it will be available in your code.
Again, JSON can do all of that.
So I would suggest a JSON file, that is included via script tag on the client side. JSON is easy to generate, read and manipulate on the server side (eg.:
json_encode,json_decodein php).It SHOULD BE a static js file, as it stresses the server the least. Also, Gzip compression can help to keep the bandwidth cost low.