OK, so what you want to do is capture data residing in HTML tables on the client side, yes? Then what? JavaScript cannot save/write data to your hard drive, it was designed that way. OK cookies are a special case; but wholely inadequate for your needs.
A couple of suggestions:
XML
eXtended Markup Language is the "wave of the future" for passing around data in a generic way. But again, this will have to be done server-side. XML involves creating custom-made html tags and surrounding your data w/ them in such a way that the data structure and other relevant info (like field names) is captured as part of the XML. Then the receiver doesn't need to know anything, theoretically about the data he/she/it received before hand.
But there is a learning curve here that may not be be necessary if:
- Your data tables / structure won't be changing.
- This really is a temporary solution
JavaScript Data Structure
That's what I did. I had some HTML table data and I read it into an array-of-arrays structure. Now I can manipulate the data any way I want, including building comma delimited strings in your case.
But, again, can't save it to disk. I built these structures for field validation and cross-fields and cross-record validation.
I did it with "dynamic" HTML, not the DOM. There are a couple of things I did that allowed me to grab the data on the client side w/out traversing a table structure using the DOM.
Name each field, i.e. <input> tag
When the table is build on the server, each <input> tag (think field) is given a name. So it might look like this on the client:
Code:
<form name="myDatabase">
<table>
. . .
<tr>
<td>
<input name="first_name" value = "Joe Swartz">Joe Swartz
</td>
. . .
</tr>
The key is that each first_name field, in this case,
has the same name. That way we can reference ALL first_name fields, automatically, all at once, as an array. In other words the following gives me a javascript array of all first_name fields:
build an array of each data structure part (field, record, table)
Code:
var firstNames = document.myDatabase.first_name;
Now to iterate through the array:
Code:
for (var i = 0; i < firstNames.length; i++) {
this_name = firstNames[i].value;
}
Thats the core. Now create arrays like this for every field.
Now you create an object who's properties (variables) match your field names.
Code:
function aRecord(fName, lName, howOld) {
this.first_name = fName;
this.last_name = lName;
this.age = howOld;
}
Don't be intimidated by the word "object". Note that it looks *exactly* like a function, but instead of "var variableName" we say "this.variableName" to create variables
Now create an array of records:
Code:
var myRecords = new Array();
for (var i=0, i<firstNames.length; i++) {
myRecords[i] = new aRecord (firstNames[i], lNames[i], ages[i]);
}
Now you have the entire HTML table in a single JavaScript structure/object, "myRecords" in this example.
Now you can build your "string of records":
Code:
var recordsString = new String();
for (var i=0; i<myRecords.length; i++) {
recordsString += myRecords[i].fName + "," +
myRecords[i].lName + "," +
myRecords[i].age +
"\n"; // end of record
}
But what can you do with it outside the immediate client code? You can't save it to disk. But you can custom-build other HTML tables from it to display on this same client. As Vladdy said, you really need to work with it server-side if you're trying to export the data.