Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

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

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.

For example

  • Project
    • WebContent
      • index.html
      • manifest.json
      • main.js
      • resource
        • config.properties

Here I want main.js to load config.properties file in the beginning and get key-value pairs.

Have anyone done something like this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...