Ciupaz
January 1, 2022, 10:24am
1
Hello,
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:
Value01
Value02
Value03
…
or in comma separated values:
Value01,Value02,Value03
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.
Luis
Welcome to this forum.
Would you like the values to be put into one JavaScript array?
Ciupaz
January 1, 2022, 11:15am
3
Could be fine.
The important is then I can put these values in variables.
To read the file you can use the Fetch API:
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
or the older XMLHttpRequest:
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Why can’t you leave the values in an array?
Ciupaz
January 1, 2022, 12:50pm
5
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:
Read the .txt file into a string using fetch
or XMLHttpRequest
;
Split the string into a string array using the string split()
method;
Get the length of the array using the string length
property;
Put your ‘if
’ statement within a loop;
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.
system
Closed
April 2, 2022, 9:22pm
7
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.