Does anyone use config files for javascript? Does anyone use config files for javascript? xml xml

Does anyone use config files for javascript?


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();


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)


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>