Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    CsV to javascript array

    Hello Colleagues

    I joined this forum yesterday. I am keen to be able to read a csv file in the same folder as my html and create a javascript array with it. I have successfully used a jquery function that puts a table on the html with the csv showing however it would be useful to create an array with this data.The code I used to accomplish this is shown below.

    <title>jQuery CSVToTable</title>
    <link rel="stylesheet" href="css/csvtable.css" type="text/css" />
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="js/jquery.csvToTable.js"></script>

    $(function() {
    $.get('11MTC_BB_09.csv', function(data) {
    $('#CSVSource').html('<pre>' + data + '</pre>');

    $('#CSVTable').CSVToTable('11MTC_BB_09.csv', { loadingImage: 'images/loading.gif', startLine: 0 });



    I thought I had asked this question yesterday but it has not appeared in the forums yet.

    Rory Barrett

  2. #2
    Senior Coder
    Join Date
    Sep 2010
    Thanked 275 Times in 275 Posts
    Do you have a server side language that can read the .csv file?
    Most can, just have it write the .js file for you. If you have .php,
    the most common server side language, it can handle .csv files
    very well.

  3. #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Thanked 641 Times in 636 Posts
    actually, your question is here

    but anyway. This is a simple AJAX request and there is no need to use jQuery if all you want is the data in an array. You may find it better to build your own array-to-table code, depending on what your needs are.

    Here's how you'd do it in plain javascript. The items in the csv file go into an array called "deets" (although you will have to check how exactly your csv file is saved,. and that will dictate how you should split the response text). Here they are just printed out into a div, but you can do what you like with them...

    <div id="results"></div>
    <script type="text/javascript">
    var deets=[];
    function getData(callback) {
    var url="11MTC_BB_09.csv"
    var csvFile = window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');
    	csvFile.onreadystatechange = function() {
       if (csvFile.readyState == 4) {
         callback(csvFile, csvFile.status);
    csvFile.open("GET", url, true); 
    getData(function(data) {
      deets = data.responseText.split(/\n/);
      for (var i = 0; i < deets.length; i++) {
    Last edited by xelawho; 10-01-2012 at 06:49 PM.

  4. #4
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Halle (Saale), Germany
    Thanked 1,213 Times in 1,183 Posts
    Quote Originally Posted by xelawho View Post
    actually, your question is here

    Ive closed that one to not let a double conversation lead to confusion.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts