简体   繁体   中英

reading a txt file line by line using JavaScript

I'm being able to print the entire file contents using:

write(document.getElementById('iframe').contentDocument.body.firstChild.innerHTML);

But how do I retrieve and print file contents line by line?

You can use the split function and the delimiter "\\r\\n" or "\\n" to extract all the individual lines in your loaded content, and then perform whatever you wish to do with them:

var text = document.getElementById('iframe').contentDocument.body.firstChild.innerHTML;
var lines = text.split("\n");            // this will extract all the lines
for (i=0; i<lines.length; i++) {         // this will move over all the lines
    var current_line = lines[i];         // this contains every line at its turn
    // do here what ever you want to do with current_line
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM