javascriptglobal

Where should I store settings for my javascript program?


I've written my first big Obj-Oriented Javascript charting application (bar charts, gantt charts, etc) and I'd like to give users the option to customize output -- things like font size, charting colors, etc.

Right now, I'm passing in a config file that contains global variables which are either A) hard-coded, or B) pulling params from the URL. (To be clear, I think its a "config" file -- its just a *.js file with a bunch of globals in in).

Is there a better technique for doing this than loading a config file into the global space? What is the "best practice" for this type of thing? Should I have a "settings" object? Or store the settings in an xml file?


Solution

  • is there a better technique for doing this than loading a config file into the global space?
    Usually, you define your own custom namespace, so your data won't interfere with data defined by any other scripts. Something like

    if (!window.my_project) {
        window.my_project = {};
    
        my_project.SOME_CONFIGURATION_VALUE = 1;
        my_project.some_function = function(){};
        ...
    }