Mark Rogers wrote:
I did look at this, but I need the ability to parse .csv files from within Javascript and I haven't seen any examples of how to do this. (It may be trivial, of-course; parsing the data ought to be OK but I don't know how to open a URL and pull data from it line-by-line in order to parse it. Is this easier than I think?)

I know of two ways to bring .csv data into JavaScript:

1) the really crude way is to wrap it in a little bit of JavaScript code to make it a string, e.g.

      var csvData = "-----put your data here-----";

and then load it as a script in your web page. You've then got your data as a global (i.e. window level) variable. That assumes you can either modify the embedded server or that you have some way to filter the returned data (e.g. pipe it through sed);

2) the modern, fashionable, way is to use the XMLHttpRequest object in your JavaScript code to fetch the data from your server, like a fancy "AJAX" web application. Despite the object name, you don't have to treat the returned data as XML, you can just use it as a string. That means you don't need to do anything to the server. XMLHttpRequest will slurp all of the data in one go but, unless you have a huge amount of it, that shouldn't be a problem.

I am fairly sure I've seen some free JavaScript libraries around for parsing CSV once you've got the data as a string. Try Googling on "JavaScript CSV parser". If the content is well behaved (no embedded quotes or control characters) it may be quite easy to write your own parser. If there are no commas embedded in the data you can even use the "split" function.

Can you modify the embedded server at all? JSON is very easy to output and can then be pulled straight into your page as a script, so that the JavaScript interpreter does the parsing for you. Alternatively, if you enjoy hacking around with the W3C DOM, you could output XML and work with that (and the best of British to you if you go that way!).

HTH

Tony Cowderoy



_______________________________________________
Peterboro mailing list
[email protected]
http://mailman.lug.org.uk/mailman/listinfo/peterboro

Reply via email to