girlswants_me
asked on
Retrieve information from any text file using javascript
Hi everyone!
anybody can help me on how to retrieve a certain information from a txt file. Like for example (Please see below txt file).
Products.txt
==============
apple 100.00 100pcs.
banana 50.00 30pcs.
orange 20.00 2pcs.
My problem now is how to retrieve the information for "banana" from the Products.txt.
Using a JavaScript, i would like to have the information of "banana" which is "50.00" and "30pcs." and displaying them.
Anybody can help me?
Thank you in advance.
anybody can help me on how to retrieve a certain information from a txt file. Like for example (Please see below txt file).
Products.txt
==============
apple 100.00 100pcs.
banana 50.00 30pcs.
orange 20.00 2pcs.
My problem now is how to retrieve the information for "banana" from the Products.txt.
Using a JavaScript, i would like to have the information of "banana" which is "50.00" and "30pcs." and displaying them.
Anybody can help me?
Thank you in advance.
You can use
var fso = new ActiveXObject("Scripting.F ileSystemO bject");
var f = fso.OpenTextFile("product. txt", 1);
var content = f.ReadAll();
This will popup a security warning because of the ActiveXObject usage.
This will return you the entire content of the text file. If you have got the contents in some kind of delimited pattern, you can then parse the contents to get any particular line.
For instance, if the columns in the file are separated by a space:
var column1 = new Array();
var column2 = new Array();
var column3 = new Array();
var lines = content.split("\n");
for(var i=0; i<lines.length; i++){
column1[i] = lines[i].split(" ")[0];
column2[i] = lines[i].split(" ")[1];
column3[i] = lines[i].split(" ")[2];
}
You can then loop through the columns arrays to get whatever value you want or use some other method like custom objects to store the data.
var fso = new ActiveXObject("Scripting.F
var f = fso.OpenTextFile("product.
var content = f.ReadAll();
This will popup a security warning because of the ActiveXObject usage.
This will return you the entire content of the text file. If you have got the contents in some kind of delimited pattern, you can then parse the contents to get any particular line.
For instance, if the columns in the file are separated by a space:
var column1 = new Array();
var column2 = new Array();
var column3 = new Array();
var lines = content.split("\n");
for(var i=0; i<lines.length; i++){
column1[i] = lines[i].split(" ")[0];
column2[i] = lines[i].split(" ")[1];
column3[i] = lines[i].split(" ")[2];
}
You can then loop through the columns arrays to get whatever value you want or use some other method like custom objects to store the data.
ASKER
Sorry, your code does'nt work. I received a RUNTIME ERROR
Error:Automation server can't create object
my code is:
<HTML>
<SCRIPT Language='JavaScript'>
function readme(){
var fso = new ActiveXObject("Scripting.F ileSystemO bject");
var f = fso.OpenTextFile("product. txt", 1);
var content = f.ReadAll();
var column1 = new Array();
var column2 = new Array();
var column3 = new Array();
var lines = content.split("\n");
for(var i=0; i<lines.length; i++){
column1[i] = lines[i].split(" ")[0];
column2[i] = lines[i].split(" ")[1];
column3[i] = lines[i].split(" ")[2];
}
}
</SCRIPT>
<BODY>
<A href="#" onclick='readme()'>click here</A>
</BODY>
</HTML>
Error:Automation server can't create object
my code is:
<HTML>
<SCRIPT Language='JavaScript'>
function readme(){
var fso = new ActiveXObject("Scripting.F
var f = fso.OpenTextFile("product.
var content = f.ReadAll();
var column1 = new Array();
var column2 = new Array();
var column3 = new Array();
var lines = content.split("\n");
for(var i=0; i<lines.length; i++){
column1[i] = lines[i].split(" ")[0];
column2[i] = lines[i].split(" ")[1];
column3[i] = lines[i].split(" ")[2];
}
}
</SCRIPT>
<BODY>
<A href="#" onclick='readme()'>click here</A>
</BODY>
</HTML>
Is it a file not found error. Try giving the full path and use either '/' or '\\' as separator.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
<html>
<body>
<script type="text/javascript">
var contents;
function showContent(){
var content = self.frames['textFile'].do cument.bod y.innerTex t;
contents = content;
}
function displaycontent(){
alert(contents.substring(c ontents.in dexOf("Wat ercress"), contents.i ndexOf("Wa tercress") +80));
}
</script>
<iframe name="textFile" src="PLufile.txt" style="display:none" onload="showContent()"></i frame>
<a href='#' onclick='displaycontent()' >click here</a>
</body></html>
<body>
<script type="text/javascript">
var contents;
function showContent(){
var content = self.frames['textFile'].do
contents = content;
}
function displaycontent(){
alert(contents.substring(c
}
</script>
<iframe name="textFile" src="PLufile.txt" style="display:none" onload="showContent()"></i
<a href='#' onclick='displaycontent()'
</body></html>
ASKER
TheSmartUno,
Thanks for the link. It works using the iframe as the object holds the text.
best regards,
Thanks for the link. It works using the iframe as the object holds the text.
best regards,
So why did I get a C? (did it not work for you?)
ASKER
Sorry, i thought that "A" should be given if the answer is directly given. I give "C" because the answer was not coming from you directly.
ASKER