You are here: irt.org | FAQ | JavaScript | file | Q1406 [ previous next ]
The following was submitted by Jesse Chisholm:
Generate your data file as a JavaScript source file, putting all your data in an array or object. This can be done by hand, or as the output of some external program.
Here is a sample dataset*.js data set file:
// usage: <script language="JavaScript" src="dataset.js"></script> // if (null==dataSet) dataSet = new Array(); dataSet[dataSet.length] = "1st new data item"; dataSet[dataSet.length] = "2nd new data item"; dataSet[dataSet.length] = "3rd new data item"; dataSet[dataSet.length] = "4th new data item"; if (null==dataObj) dataObj = new Object(); dataObj['a'] = 'apple'; dataObj['b'] = 'banana'; // In the main flow of your JavaScript, the array dataSet // and the object dataObj will be available for use.
In the main HTML file:
<html> <head> <script language="JavaScript"> dataSet = new Array() dataObj = new Obj() </script> <script language="JavaScript" type="text/javascript" src="dataset.js"></script> </head> <body> <script language="JavaScript" type="text/javascript"><!-- for(var i=0; i<dataSet.length; i++) { document.write( "<p>" + dataSet[i] + "<\/p>" ); } document.write( "<p>" + dataObj['a'] + "<\/p>" ); document.write( "<p>" + dataObj['b'] + "<\/p>" ); //--></script> </body> </html>