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;
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?
Modularize — one function per task This is a general programming best practice — making sure that you create functions that fulfill one job at a time makes it easy for other developers to debug and change your code without having to scan through all the code to work out what code block performs what function.
By default, browsers expect JavaScript. It is, however, best practice to always specify the language. This is done by setting the attribute "language" in the <script> tag to "JavaScript". Lastly, you will need to insert the JavaScript code between the <script> and </script> tags surrounded by comment tags, <!
Optimize JavaScript Placement Another good practice in using JavaScript with HTML is to place your JavaScript at the end of your HTML file if it is possible, between <script> tags, before the closing </body> tag, as you may notice from the above example.
To add a script to your page, start by inserting these tags. In many cases, you'll put the <script> tags in the page's <head> in order to keep your JavaScript code neatly organized in one area of the web page. However, it's perfectly valid to put <script> tags anywhere inside the page's HTML.
- Must define a global variable or json or javasctipt object or array (I don't know which is best)
JSON is basically an object literal, so it can do both. Go for it. Think of JSON as a serialized javascript object.
- This values must reachable by other javascript functions and objects.
As soon as you run the JSON it will be available in your code.
- 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.)
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_decode
in 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With