开发者

Best-practice for javascript configuration on new web project

开发者 https://www.devze.com 2023-01-05 16:37 出处:网络
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 projec

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 javascr开发者_开发知识库ipt - 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号