views:

144

answers:

5

We have javascript files that are environment specific, and so I was thinking of going down the path of creating a generic way to read in an XML (config) file to store different environment specific settings. I was curious to know if anybody else on here does that (or if not, is there a reason why you don't)?

A: 

You can easily pull down XML files using something like jquery (http://think2loud.com/reading-xml-with-jquery/).

However, I'd like to pose whether environment specific client-side javascript code is really a good idea. Seems like any environment specific (ie. qa, uat, production I assume) should be handled on the server and the client should be environment agnostic

Joel Martinez
It's currently for an environment specific AJAX call. The URL will be different depending on the environment since our different web apps have a different URL depending on the environment. I suppose that with this little variance, the path I'm going down could be overkill, but it was something I wanted to explore.
Ek0nomik
+7  A: 

JSON is hundreds of times faster to consume than XML, bring a native JavaScript object itself. Attach and forget.

EDIT:

James Westgate's example is JSON. You can use this inline or as an external file or even loaded via AJAX.

Here is another example:

var clientData = {}
clientData.dataset1 = [
    {name:'Dave', age:'41', userid:2345},
    {name:'Vera', age:'32', userid:9856}
]

alert(clientData.dataset1[0].name)
Diodeus
I've never used JSON before, but I wouldn't mind giving this a shot. I have a small sample JSON file built, but I'm not sure what the best way to convert that into a Javascript object is. Do you have any simple examples?
Ek0nomik
this is not valid JSON.
Jason
Sloppy edit. Sorry.
Diodeus
+5  A: 

All you need to do is load a javascript file with some variable definitions, ideally namespaced. You can use a single object literal for this:

var config = {
  option1: 'good;', 
  option2: {name: 'bad'}, 
  option3: ['u','g','l','y']
}

Then load that file as your first script and you will have access to config information in each subsequent script eg

if (config.option1 == 'good') doStuff();
James Westgate
What do you mean when you say namespaced?
Ek0nomik
prefix the variable with a function or other variable to give it more meaning/ context eg config.option1 instead of just option1
James Westgate
+1  A: 

Why not use a separate js file to store your environment-specific settings?

Just like you can use multiple CSS files to style your page, you can use multiple js files as well.

So you could create a file called app-config.js with specific settings:

var SiteName = "MyWebsite.com";
var HeaderImage = "http://mycdn.com/images/mywebsite/header.png";

And then you include the js on your pages like this:

<script type="text/javascript" src="/js/app-config.js"></script>
<script type="text/javascript" src="/js/app.js"></script>
Steve Wortham
+1  A: 

One thing you might consider is to have the pages themselves include little Javascript blocks to provide just this sort of configuration information. Often, you really only need a small amount of information for URL building etc. I'll give a JSP example:

<script>
  var appConfig = {
    'syndicate': '${environ.syndicate}',
    'urlBase': '${environ.urlBase}'
  };
</script>

Then your "pure" Javascript code can look to window.appConfig to get critical information. Obviously this would get to be a mess if you needed a whole lot of stuff.

This is particularly easy when your pages are built via some templating system in your server-side environment. That way, you only have to set the script tag up in one place (or a small number of places; the templates in other words) and all the pages get it automatically.

(edited to eliminate weird variable declaration)

Pointy
Why `window['appConfig']`? `var appConfig` works just as well.
Christopher Parker
Oh yes you're right of course; it's just a cargo cult habit. I'll edit the answer.
Pointy