0

I am html javascript beginner and i simply have to read the contents of a text file at a location and store each column in that file in an array (there are 3 columns) in html and i use javascript to do so.

I have came across 2 errors http://prntscr.com/6ilvhx :

(1)ReferenceError: require is not defined

(2) ReferenceError: readfile is not defined

My code to do this is :

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script>
        console.log("just a check1");
        var fs = require('fs');
        console.log("just a check2");

        function readLines(input, done) {
            var arr = [];
            var remaining = '';

            input.on('data', function (data) {
                remaining += data;
                var index = remaining.indexOf('\n');
                while (index > -1) {
                    var line = remaining.substring(0, index);
                    remaining = remaining.substring(index + 1);
                    func(line);
                    index = remaining.indexOf('\n');
                }
            });

            input.on('end', function () {
                if (remaining.length > 0) {
                    func(remaining);
                    done(arr);
                }
            });

            function func(data) {
                arr.push(data.split(/\s+/g));
            }
        }

        var input = fs.createReadStream('Content\TextFile\distinctGimeiNo.txt');
        readLines(input, done);

        function done(arr) {

            var obj = {};
            var key1 = arr[0][0];
            var key2 = arr[0][1];
            var key3 = arr[0][3];
            obj[key1] = [];
            obj[key2] = [];
            obj[key3] = [];

            arr.shift();

            arr.forEach(function (item) {
                obj[key1].push(item[0]);
                obj[key2].push(item[1]);
                obj[key3].push(item[2]);
            });

            console.log('X:', obj['X']);
            console.log('Y:', obj['Y'])
            console.log('Z:', obj['Z'])
        }
    </script>

</head>
<body>
    <h1>Hello World!</h1>    
</body>
</html>

How to make it work ?I found this on debugging http://prntscr.com/6ilvhx

Updated code : (after adding library)

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src=http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js>

        console.log("just a check1");
        var fs = require('fs');
        console.log("just a check2");
        function readlines(input, done) {
            var arr = [];
            var remaining = '';
            input.on('data', function (data) {
                remaining += data;
                var index = remaining.indexOf('\n');
                while (index >
        -1) {
                    var line = remaining.substring(0, index);
                    remaining = remaining.substring(index + 1);
                    func(line);
                    index = remaining.indexOf('\n');
                }
            });

            input.on('end', function () {
                if (remaining.length > 0) {
                    func(remaining);
                    done(arr);
                }
            });

            function func(data) {
                arr.push(data.split(/\s+/g));
            }
        }

        var input = fs.createReadStream('Content\TextFile\distinctGimeiNo.txt');
        readLines(input, done);

        function done(arr) {

            var obj = {};
            var key1 = arr[0][0];
            var key2 = arr[0][1];
            var key3 = arr[0][2];
            obj[key1] = [];
            obj[key2] = [];
            obj[key3] = [];

            arr.shift();

            arr.forEach(function (item) {
                obj[key1].push(item[0]);
                obj[key2].push(item[1]);
                obj[key3].push(item[2]);
            });

            console.log('X:', obj['X']);
            console.log('Y:', obj['Y'])
            console.log('Z:', obj['Z'])
        }
    </script>



</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>
1
  • which libraries do you use? in your code, there is no reference to them - how should the browser include em, if he don't knows about em... Commented Mar 19, 2015 at 8:26

1 Answer 1

2

Impossible to use node.js script in your browser, start it with node.. :)

You can use jQuery for do that in client side :

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script>
     console.log("just a check1");
     $.ajax({
         type:'GET',
         url:'./hello.txt',
         dataType: 'text',
      }).success(function (data){
               s = data.substr(32);
               var s2 = s.replace(/\r\n/g," ");
               var array = s2.split(' ');

               var col1 =[];
               var col2 =[];
               var col3 =[];
               var j=0;
               for(var i = 0; i <= array.length-3; i=i+3){
                       col1[j]=array[i];
                       col2[j]=array[i+1];
                       col3[j]=array[i+2];
                       j++;
               };

               console.log(col1,col2, col3);
      })
 </script>

</head>
<body>
  <h1>//hello world !</h1>
</body>
</html>

$.ajax allow you to get back datas with a specify url. Normally,that do that you wanted

Sign up to request clarification or add additional context in comments.

7 Comments

please explain a bit more in detail.. I am new to javascript (switched from c++) . How to do and what to do to make it work ?
Sorry ^^, you use a script for server-side in client side. i think it's impossible, you need to create file (ex : server.js) and put your lines into this file. Then you need to launch shell and launch your server file with that command : node server.js (If you don't have node.js, you must download it)
Actually i have no idea about client and server side. I have just switched from C++ . I am just added a html page and run it in browser. But could u please let me know Ho to make it work. I have to read a file and store its contnt in arrays. Is it poasible how to do that.
Oki, here you can use javascript for client and not node.js script (for server), try this : $.ajax({ url: "path", success: function (data){ var obj = data; console.log(obj.color) //example } });
You need to include <script type="text/javascript" src="ajax.googleapis.com/ajax/libs/jquery/1.8/…> in your html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.