Where to put frontend javascript configuration variables
I ended up writing a conf.js file with content
window.CONFIG = {
SOME_CONSTANT: 22,
}
and included it in a new <script>
in my index.html before the other scripts.
The window
is not mandatory but shows where it comes from when I call that as window.CONFIG
anywhere in the rest of the javascript.
CONFIG = (function(){
var conf_info = {};
conf_info["url"] = 'http://codepen.io/pen/';
return{
getValue : function(param){
return conf_info[param];
}
}
})();
//some where in different file
document.getElementById("result").innerHTML = CONFIG.getValue('url');