Forum Moderators: open

Message Too Old, No Replies

open text file on server

         

Skier88

7:41 am on Sep 9, 2009 (gmt 0)

10+ Year Member



I want to use javascript to scan a text file on my website for a certain phrase, and report the results to the current page. How do I open the file? Do I have to read it all into a var to scan it, or can you read sequentially like in java? Thanks for reading.

StoutFiles

8:09 am on Sep 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://my.remote.url/myremotefile.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n"); // Will separate each line into an array
}
}
}
txtFile.send(null);

Skier88

8:15 am on Sep 9, 2009 (gmt 0)

10+ Year Member



Thank you. Does this work with all browsers?

StoutFiles

9:08 am on Sep 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll have to test that on your own.

I don't use this code as PHP is 100% more reliable for opening and parsing text files.