Click to See Complete Forum and Search --> : textfile Javascript
lucpustjens
01-09-2004, 06:09 AM
I am trying to read a textfile in Javascript, line for line.
Each line has one value to put in a variable:
for example somthing like this:
document.form.user.value = textfile.readline
document.form.***.value = textfile.readline
Is this possible? And how can I open and read a textfile?
(I Dont want to display the textfile)
Thanks a lot
Khalid Ali
01-09-2004, 06:42 AM
you can not read any files using JavaScript but these
*.css
*.js
*.xml
You will have to use some sort of ActiveXControl or Java Applet to read fiels on a system using JavaScript
TheBearMay
01-09-2004, 06:58 AM
In a pure sense you can't do it...however... You can use ActiveX objects to do it, but I'm not sure that they'll work for all browsers . You can also DOM objects for XML files, again browser compatibility issues. Also could use a server side application or a Java Applet to feed your script...
Still another way, is to reference the textfile as an external JS script file - of course all statements then must follow JS standards. (You can dynamically chance the SRC value of the external script if you need to reference different sets of values.)
Currently for reading text and HTML files, I just wrote a quick Java Applet. (If you want it, let me know and I'll document real quick and upload it.)
TheBearMay
01-09-2004, 07:01 AM
Note: I type a LOT slower than Khalid Ali. LOL.
lucpustjens
01-09-2004, 07:12 AM
Thanks for help!
TheBearMay
01-09-2004, 07:40 AM
Had a couple of requests so....
TheBearMay
01-09-2004, 08:00 AM
Forgot to include one more exposed method.
getLineNum() - Returns line number of the last line read. After a call to getText()/getHTML() this is equal to the number of lines in the file.
Replacement document attached.
said_fox
02-02-2004, 06:01 PM
:) This discution induced an idea to my mind!
Ok, javaScript can not read from an external text file and some compatability issues about DOM and ActiveX.
Good.
It can read from a *.js file. Wow. That's enough and more easy to be handled.
What's happened if we write *.js file to be included in our script instead of text file
psedoTXTFile = new Array();
psedoTXTFile[1] = "Line 1 of our text file";
psedoTXTFile[2] = "Line 2 of our text file";
psedoTXTFile[3] = "Line 3 of our text file";
psedoTXTFile[4] = "Line 4 of our text file";
psedoTXTFile[5] = "Line 5 of our text file";
Now it seems to be easy to handle and change the data which will be used in the script away from the original script.