Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best-practice for javascript configuration on new web project

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?

like image 393
edigu Avatar asked Jul 06 '10 10:07

edigu


People also ask

What is an example of a best practice for using JavaScript in your project?

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.

What are some best practices for incorporating JavaScript in a web page?

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, <!

What is the best practice for placing JavaScript in an HTML document?

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.

What is the correct way to implement a script on your website?

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.


1 Answers

  • 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.

like image 168
25 revs, 4 users 83% Avatar answered Nov 02 '22 15:11

25 revs, 4 users 83%