qwerty123
qwerty123

Reputation: 131

How can I parse a text file using javascript

The code below is to read a text file using javascript. it works. However, I just want to read part of the content. For example, the content of the file is :"Hello world!" I just want to display "Hello". I tried function split(), but it only works on strings. I don't know how to insert it here.

 var urls = ["data.txt"];

function loadUrl() {
    var urlToLoad = urls[0];
    alert("load URL ... " + urlToLoad);
    browser.setAttributeNS(xlinkNS, "href", urlToLoad);
}

thank you!!!

Upvotes: 13

Views: 90914

Answers (3)

randomizer
randomizer

Reputation: 1649

Try this to read separate words if I understood correctly what you need. Create a file with the contents "hello world" and browse to it with the example script. The output is "hello".

<html>
<head>
<input type="file" id="fileinput" />
<script type="text/javascript">
  function readSingleFile(evt) {
    var f = evt.target.files[0];   
    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;             
          var ct = r.result;
          var words = ct.split(' ');            
          alert(words[0]);
      }
      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    }
  }

  document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
</script>
</head>
<body>
</body>
</html>

Reading directly has to be with an ajax request due to the javascript restrictions regarding safety. This code shoudl perform the requested operation:

<html>
<head>
<input type="file" id="fileinput" />
<script type="text/javascript">
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
  if(xmlhttp.status==200 && xmlhttp.readyState==4){    
    var words = xmlhttp.responseText.split(' ');
    alert(words[0]);
  }
}
xmlhttp.open("GET","FileName.txt",true);
xmlhttp.send();
</script>
</head>
<body>
</body>
</html>

Upvotes: 12

Padmanathan J
Padmanathan J

Reputation: 4620

I used

jQuery.get('http://localhost/foo.txt', function(data) {
var myvar = data;
});

, and got data from my text file.

Or try this

JQuery provides a method $.get which can capture the data from a URL. So to "read" the html/text document, it needs to be accessible through a URL. Once you fetch the HTML contents you should just be able to wrap that markup as a jQuery wrapped set and search it as normal.

Untested, but the general gist of it...

var HTML_FILE_URL = '/whatever/html/file.html';

$(document).ready(function() {
    $.get(HTML_FILE_URL, function(data) {
        var fileDom = $(data);
        fileDom.find('h2').each(function() {
            alert($(this).text());
        });
    });
});

Upvotes: 15

edi9999
edi9999

Reputation: 20544

Opening a file in javascript with ajax (without using any framework)

var urls = ["data.txt"];
xhrDoc= new XMLHttpRequest();   
xhrDoc.open('GET', urls[0] , async)
if (xhrDoc.overrideMimeType)
    xhrDoc.overrideMimeType('text/plain; charset=x-user-defined')
xhrDoc.onreadystatechange =function()
{
if (this.readyState == 4)
{
    if (this.status == 200)
   {
    var data= this.response; //Here is a string of the text data 
   }

}                   
}
xhrDoc.send() //sending the request

Upvotes: 0

Related Questions