0

So i have text file which contains some numbers and i want so display all that content in html page. I assume i need to use javascript or jQuery. I tried things like:

function getText() {
  fetch('inputfile.txt')
    .then(function(res) {
      return res.text();
    })
    .then(function(data) {
      console.log(data);
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <title></title>
  <style type="text/css" media="screen">

  </style>
  <script type="text/javascript" src="jquery-1.11.3.min.js"></script>
  <script type="text/javascript">
    $(function() {
      $.get('inputfile.txt', function(data) {
        //If the returned data is not empty or whitespace
        if (data) {
          // split the string into several strings when a new line occures
          var lines = data.split('\n'); // Take note of the \n This is the new line character also known as a line ending, end of line (EOL), or line break.
          // If there are more than 0 lines
          if (lines.length > 0) {
            // For every line console.log out what the line is with the line number prepended.
            for (var i = 0; i < lines.length; i++) {
              console.log(i + ": " + lines[i]);
            }
          }
        } else {
          // It is empty, do somethin else
          alert('no data present in txt file.');
        }
      });
    });
  </script>
</head>

<body>
</body>

</html>

But every time i check webpage it not showing any content from txt file.

Ashu
  • 2,066
  • 3
  • 19
  • 33
Vil4nte
  • 13
  • 2

1 Answers1

0

I included comments to the code I posted, add a content area I put in a 'pre' tag but it could be anything, then in the code when you receive the response load it into the html of that tag/container:

<html>
<head>
    <title></title>
    <style type="text/css" media="screen">
    </style>
    <script type="text/javascript" src="jquery-1.11.3.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $.get('inputfile.txt', function(data) {
                //If the returned data is not empty or whitespace
                if (data) {
                    // split the string into several strings when a new line occures
                    var lines = data.split('\n'); // Take note of the \n This is the new line character also known as a line ending, end of line (EOL), or line break.
                    // If there are more than 0 lines
                    if (lines.length > 0) {
                        // For every line console.log out what the line is with the line number prepended.
                        var $content = $('#content'); //grab the content area
                        for (var i = 0; i < lines.length; i++) {
                            console.log(i + ": " + lines[i]);
                            $content.html($content.html() + "\n" + i + ": " + lines[i]); //insert a new line into the content area
                        }
                    }
                } else {
                    // It is empty, do somethin else
                    alert('no data present in txt file.');
                }
            });
        });
    </script>
</head>
<body>
  <pre id="content"></pre><!-- Added a place for the content --> 
</body>
</html>