简体   繁体   English

如何在JavaScript中读取配置文件(.properties)中的信息?

[英]How to read the information in configuration files(.properties) in JavaScript?

In my code, I need to get data from configuration file(.properties) .My configuration file looks something like this(.properties): 在我的代码中,我需要从配置文件(.properties)获取数据。我的配置文件看起来像这样(.properties):

maxTime = 60
upVotePostMaxTime=60

but I don't know how to read the configuration file in JavaScript. 但我不知道如何在JavaScript中读取配置文件。 Is there a better way of doing this? 有没有更好的方法呢?

It is not advisable to read configuration files from JavaScript. 不建议从JavaScript读取配置文件。 Instead you could do this: 相反,你可以这样做:

  1. Read the file on server-side (using PHP, .Net or Python or any other language that you prefer) 在服务器端读取文件(使用PHP,.Net或Python或您喜欢的任何其他语言)
  2. Render the valid values to JavaScript. 将有效值呈现给JavaScript。 Something like: 就像是:

    var maxTime = "<?php echo maxTime; ?>";

    var upVotePostMaxTime = "<?php echo upVotePostMaxTime; ?>";

where <?php echo maxTime; ?> 其中<?php echo maxTime; ?> <?php echo maxTime; ?> and <?php echo upVotePostMaxTime; ?> <?php echo maxTime; ?><?php echo upVotePostMaxTime; ?> <?php echo upVotePostMaxTime; ?> will have values 60 each. <?php echo upVotePostMaxTime; ?>每个值为60 This would show on the client-side as: 这将在客户端显示为:

var maxTime = "60";
var upVotePostMaxTime = "60";

and then you may use it in JavaScript. 然后你可以在JavaScript中使用它。

Values are assigned in double quotes to prevent breaking of JavaScript code in case null values are sent from the server. 值以双引号分配,以防止在从服务器发送null值的情况下破坏JavaScript代码。

Please ensure that proper values are assigned to JavaScript variables. 请确保为JavaScript变量分配了正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM