How to read a properties file in javascript from project directory?

There is a super simple way to do this, along the lines of sowbug’s answer, but which doesn’t need any XHR or file reading.

Step 1.
Create resource/config.js like so:

gOptions = {
  // This can have nested stuff, arrays, etc.
  color: 'red',
  size: 'big',
  enabled: true,
  count: 5
}

Step 2.
Include this file in your index.html:

<!doctype html>
<head>
  <script src="https://stackoverflow.com/questions/19310951/resource/config.js"></script>
  ...

Step 3.
Access your options directly from your main.js (or anywhere):

  ...
  if (gOptions.enabled) {
    for (var i = 0; i < gOptions.count; i++) {
      console.log(gOptions.color);
    }
  }
  ...

Leave a Comment