Results 1 to 4 of 4

Thread: JavaScript to read Text fill in a table

  1. #1
    Join Date
    Aug 2013

    JavaScript to read Text fill in a table

    I am trying to make the javascript read txt file which is working, and I want it to add each row in the txt file in a Column in the table and after 3 Columns start a new row in the table and so on. also while the javascript reading my txt and once it sees \n it moves to the next row in text file and add it to a column in the table. Then display it in the div

    Here is what I have


    HTML Code:
        if (window.XMLHttpRequest) {
        //for firefox, opera and safari browswers
        var txtFile = new XMLHttpRequest();
        if (typeof XMLHttpRequest == "undefined")
        XMLHttpRequest = function () {
        try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
        catch (e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
        catch (e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {}
        throw new Error("This browser does not support XMLHttpRequest.");
        // var allText = txtFile.responseText;
        function clickyHere(){
        var info = txtFile.responseText;
        var infoArray = [];
        infoArray = info.split(/,|,+\s|\s+,\n|\r/);
        var tableCount = info.split(/,|\$/g).length;
        var rowCount = info.split(/\r|[\r]/g).length;
        var endLine = info.split(/\$/g).length;
        var newLine = info.replace(/\r\n/g,'\n').length;
        var columnTry = tableCount/endLine;
        document.write("<table border='1' columns='"+columnTry+"'>");
        var count;
        count = 0;
        for (i=0;i<infoArray.length;i++){
        x = infoArray[i];
        document.write("<td>"+x+" ");
        if (count != columnTry){
        count ++;
        if (count >= columnTry){
        count = 0;
        <div id="test" onload="clickyHere"></div>

    HTML Code:
        1, 1, aaa \n
        2, 2, aaa \n
        3, 3, aaa \n
        4, 4, aaa \n
        5, 5, aaa \n
        6, 6, aaa \n
        7, 7, aaa \n

  2. #2
    Join Date
    Sep 2008
    Akron, OH
    JavaScript cannot write to files. You'll need to use a server side language.
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Aug 2013
    I am not writing I am only reading the text file and viewing it in a table.

  4. #4
    Join Date
    Oct 2010
    Versailles, France
    It's probably possible to work directly on your info string with replace (and without split) to insert a HTML table in your page and to work with this DOM Table for your transformation.

    Don't use document.write !
    But insert your script at the end of the body after a <div id="result"></div>.
    Then a document.getElementById('result').innerHTML=myTableString; in the script is call when the div exists.

    A replace by '</td></tr><tr><td>' on each new lines (\r\n or\n or \r - see this page) and a replace by '</td><td>' on each comma, with CSV, or tab (\t), will probably build a good string to insert between '<table><tr><td>' and '</tr></table>';.

    All depends on the form of your text files. Some tests will probably allow it to finalize...
    Last edited by 007Julien; 08-10-2013 at 07:15 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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