Read text file and create dynamic variables

in my ASP.NET WebForms application I have a folder Scripts where there are
a JavaScript file and a TXT file.

This TXT file contains some values, that I can insert on it how I want.
For example, one value for row:


or in comma separated values:


Now my question is:

is it possible, in the JavaScript file, read this TXT file and create
as many variables as the values in the TXT file?

For example, in this case, something like this:

var myValue01 = Value01;
var myValue02 = Value02;
var myValue03 = Value03;

Thanks in advance for every idea.


Welcome to this forum.

Would you like the values to be put into one JavaScript array?

Could be fine.
The important is then I can put these values in variables.

To read the file you can use the Fetch API:

or the older XMLHttpRequest:

Why can’t you leave the values in an array?

Ok, I can leave in an array.
Because then I have to use these values in some IF statements.

For example, this is one if my IF

if (DataSourceNameVal == SRC_FOCUS
          || DataSourceNameVal == SRC_VAX
          || (DataSourceNameVal == Value01 && SMSExpenseId.startsWith("A-"))

Instead of Value01
I should replace with all values from the array.

I am not fully understanding what you are trying to do, but I understand there can be any number of values within your .txt file.

I suggest you:

  1. Read the .txt file into a string using fetch or XMLHttpRequest;
  2. Split the string into a string array using the string split() method;
  3. Get the length of the array using the string length property;
  4. Put your ‘if’ statement within a loop;
  5. Instead of Value01 within your ‘if’ condition, use Value[index] to get a value from the array (as a string) where Value is the string array and index is the loop index.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.