Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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());
        });
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...