Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read from text files 1

Status
Not open for further replies.

joelmac

Programmer
Jun 4, 2002
92
CA
Hi there,

Is there a way to read data from text files using javaScript? If I want to display products and prices for example. Can I have a comma separated text file something like this:

cats, $3.00
oxen, $7.00
yaks, $5.00

then open the file and loop through it and display the information in a table or something.

Thanks

________________________
JoelMac
 
NOPE, u cannot touch any files on the client's machine, this is for security purpose...

Known is handfull, Unknown is worldfull
 
What about files on the server? stored in the same directory as the page that's reading it?



________________________
JoelMac
 
Still NO...

This is because javascript cannot touch files on the server (if it can no one will bother with ASP/PHP....)

Known is handfull, Unknown is worldfull
 
Since JavaScript can already be in a separate file, why not just create a Javascript file with the items already placed in an array ready for processing.

Clive
 
thread216-569239

____________________________________________________
[sub]get the best answer to your questions by asking the best questions "General FAQ" faq333-2924[/sub]
onpnt2.gif
[sub][sup][/sup][/sub]
 
Shouldn't XML and XSLT be the answer to your problem?
<store>
<product>
<name>product1</name>
<price>4.00</price>
</product>

<product>
<name>product2</name>
<price>124.00</price>
</product>

<product>
<name>product3</name>
<price>500.00</price>
</product>
</store>

grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Shouldn't XML and XSLT be the answer to your problem?
<store>
<product>
<name>product1</name>
<price>4.00</price>
</product>

<product>
<name>product2</name>
<price>124.00</price>
</product>

<product>
<name>product3</name>
<price>500.00</price>
</product>
</store>

grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
yes that would work, and I was think of that, but the price list that I get comes in a text file.

I think that the file is exported from excel. Does anyone know if Excel can export data in an xml format?

________________________
JoelMac
 
you could load that list in Excel and from there save it as XML using the &quot;Save As&quot; option.
however the code you'll get is not going to be pretty.



grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Perhaps you could provide a sample of the price list format that you get now. I think I could show you a lot neater way of converting it into a web usable form.

Clive
 
Wait a second... you can load plain text files in a window, right? So you should be able to read the contents of it just like you would do with a web page. I don't have a web server where I'm sitting today, but I tested this on my local machine and it worked. Maybe you can use it.

<iframe name=&quot;ifrm&quot; src=&quot;text.txt&quot;
onload=&quot;alert(frames.ifrm.document.body.innerText);&quot;></iframe>
 
Excel can export as XML but I use a tool called trans2xml. I think I got it from Cnet.

I think the best way to go would be to load the data as an external .js file with:
mydata = &quot;thisisallmydata&quot;;

You can then put the script which deals with the mydata variable wherever you want.

A common programming error:
if (!IknowHow) {answer=&quot;can't be done&quot;}
 
Excel can save as CSV directly. Then, <b>server side</b> script can read that and format it into a table.

The XML that you get from Excel has so much extra goop in it that it's virtually useless. :-(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top