0

I'm currently working on creating a website that needs to access the text stored in a text file and parse it into a data structure. I would like to use a javascript function or an html tag to include or call the file so that I can parse it. However, I can't seem to find a good way to do it. Any suggestions?

I'm not sure how to be more specific, but perhaps some pseudocode might help. Current code would be along these lines:

mylist = new Array();

function myfunc()
{
    var str = [get information from website];
    mylist.push(str);
}

That is, if the website has the following text:

"<tag attr=str attr=str>text</tag>"

The newest (or last) element of mylist would be "<tag attr=str attr=str>text</tag>".

Thanks!

edit: Thanks for the quick responses. I was trying to steer away from jquery (actually, I'm implementing angular in the site), but I was trying to use a native javascript method as opposed to a library. Judging from the responses, there isn't one. Thanks for your help!

6
  • You have to use AJAX to read data from web pages in Javascript. Commented Jan 26, 2013 at 0:46
  • Or use oldschool iframe Commented Jan 26, 2013 at 0:46
  • @b1- Don't use oldschool methods in programming, it's not as cool as driving oldschool cars, and it makes web's progress slower. Iframe doesn't have any single advantage over Ajax Commented Jan 26, 2013 at 0:54
  • What have you tried so far? The comments above are gently pointing out that the "Ajax" functions of the jQuery javascript library are all ways of using Javascript's XmlHttpRequest to request a file and make it available in JS for parsing. The fact that you haven't at least found XHR so far makes me wonder how much research you've already done. Commented Jan 26, 2013 at 1:05
  • @pjmorse- Actually, I spent a couple of hours trying to do anything similar, and it was getting a little frustrating. Closest thing I found was embedding. I guess I just didn't know how to phrase it correctly in searching the web. Commented Jan 26, 2013 at 1:43

3 Answers 3

1

You should use the XHR Object, aka the "AJAX" method.

Here's an overview as to how to start using it.

Here's an excerpt:

function makeRequest(url, callback) {
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
  }
  //i'd omit this if I don't support older browsers
  else if (window.ActiveXObject) { // IE
    try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!httpRequest) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  httpRequest.onreadystatechange = function () {
    if (httpRequest.readyState === 4 && httpRequest.status === 200) {
      callback(httpRequest.responseText);
    } else {
      alert('There was a problem with the request.');
    }
  }
  httpRequest.open('GET', url);
  httpRequest.send();
}
//using
makeRequest('path/to/your/file', function (contents) {
  myList.push(contents);
});
Sign up to request clarification or add additional context in comments.

Comments

0

You could use jQuery $.get()

$.get('test.txt', function(data) {
  $('.result').html(data);
});

see http://api.jquery.com/jQuery.get/ for more details.

Comments

0

Use Ajax to load content from server in Your JS code.

Your [get information from website]; should be replaced with function that fetch data.

Consider using jQuery for this jQuery Docs on $.get()

or use native JavaScript ajax MDN docs on Ajax

2 Comments

You do notice that the OP never asked for jQuery, or even tagged it. Also, w3schools is not a recommended reference site.
OP has tagged HTML and this have nothing to do with HTML, I'm just providing him some information and alternatives

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.