How to read information in configuration files (.properties) in JavaScript?

In my code, I need to get data from a configuration file (.properties). My configuration file looks something like this (.properties):

maxTime = 60 upVotePostMaxTime=60 

but I do not know how to read the configuration file in JavaScript. Is there a better way to do this?

+4
source share
1 answer

It is not recommended to read configuration files from JavaScript. Instead, you can do this:

  • Read the file on the server side (using PHP, .Net or Python or any other language that you prefer)
  • Display valid JavaScript values. Sort of:

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

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

where <?php echo maxTime; ?> <?php echo maxTime; ?> and <?php echo upVotePostMaxTime; ?> <?php echo upVotePostMaxTime; ?> will matter 60 each. This will be displayed on the client side as:

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

and then you can use it in javascript.

Values ​​are assigned in double quotation marks to prevent JavaScript from breaking if null values ​​are sent from the server.

Verify that the JavaScript values ​​are set to the correct values.

0
source

Source: https://habr.com/ru/post/1499301/


All Articles