13

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!!!

7
  • 1) Clarify to yourself what you mean by parsing. 2) Write the code to do it. Commented Jul 15, 2013 at 7:33
  • if you read a line and put it in a string you can use split.. Commented Jul 15, 2013 at 7:41
  • it means seperate the content into parts. Commented Jul 15, 2013 at 7:41
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. Commented Jul 15, 2013 at 7:41
  • @randomizer do you know how to make the content of the text file into string? Commented Jul 15, 2013 at 7:42

3 Answers 3

15

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());
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

12

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>

2 Comments

Thank you. I tried similar way before.I can do the parsing in this way. But i need to display the text directly instead of choosing it by user.
Reading directly with javascript isn't that easy, you have to perform a XMLHttpRequest to retrieve the contents. I updated my answer with an example.
0

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

Comments

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.