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

前端 未结 5 1471
你的背包
你的背包 2020-12-05 15:23

I\'m building a Chrome Packaged App. I want to put the script configuration if a config file in a resource directory and on startup want to read that by Javascript.

5条回答
  •  广开言路
    2020-12-05 15:49

    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:

    
    
      
      ...
    

    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);
        }
      }
      ...
    

提交回复
热议问题