how to use javascript in html to read txt file and display it ?
I am newer ,pls help me achieve the following functions,
1>use javascript in html to read local txt file (when client browser open this html,start to read local txt file)
2>find all these lines including "@" from the txt file.
3>display the lines including "@" in client browser(when he open this html)
that'all,I think it is easy to do it,but i don't know,looking forwards to your help(best to give me entire codes,very appreciate ),thanks very much.
I am newer ,pls help me achieve the following functions,
1>use javascript in html to read local txt file (when client browser open this html,start to read local txt file)
2>find all these lines including "@" from the txt file.
3>display the lines including "@" in client browser(when he open this html)
that'all,I think it is easy to do it,but i don't know,looking forwards to your help(best to give me entire codes,very appreciate ),thanks very much.
The forum is not a coding service. You need to show your attempts and we can comment on them.
Read the 'stickies' at the main site about homework assignments.
Note, JS cannot read a text file on a local computer (with the exception on only one browser)
The most common access to a text file is from an internet server using ajax code logic.
i can't get answer ,can u give me suggestion how to replace fopen and fread using js same command? acutally i have a confuse,because in this link : http://www.ehow.com/how_5996745_read...avascript.html ,it said it is js command.
looking forwards to your help,thanks.
i can't get answer ,can u give me suggestion how to replace fopen and fread using js same command? acutally i have a confuse,because in this link : http://www.ehow.com/how_5996745_read...avascript.html ,it said it is js command.
looking forwards to your help,thanks.
Probably because it only works on MSIE.
What browser and version are you using?
don't listen to people who don't know how to do things...
Code:
<!DOCTYPE html>
<html>
<head>
<title>reading file</title>
<script type="text/javascript">
var reader = new FileReader();
function readText(that){
if(that.files && that.files[0]){
var reader = new FileReader();
reader.onload = function (e) {
var output=e.target.result;
//process text to show only lines with "@":
output=output.split("\n").filter(/./.test, /\@/).join("\n");
document.getElementById('main').innerHTML= output;
};//end onload()
reader.readAsText(that.files[0]);
}//end if html5 filelist support
}
</script>
</head>
<body>
<input type="file" onchange='readText(this)' />
<div id="main"></div>
</body>
</html>
Hi,
i tried ur example code and it worked.. i need some modification in dat..rite nw u r giving the user to browse for a file and based on the selected file the operation is performed.. but i want my script to always read from the same file..der is no option to browse for the file.when the page loads this function shud be called automatically and the hardcoded file shud be read. can u help me out with this please..
Thanks,
Hi,
i want my script to always read from the same file..der is no option to browse for the file.when the page loads this function shud be called automatically and the hardcoded file shud be read
that is not allowed by any browser.
if you have the file on your server, next to an html file, then you can use simple ajax to grab the file's contents. search for a basic ajax demo for that code.
Thanks for the reply.. actually i have urgent req for it.. i will tell u the full scenrio..if der is another way plz tell me dat..
i am using unix server.my html script is located in /var/www/etc/html path and the file containing the names is also stored in the same place.. i want to read this file n store all the names in an dropdown combo box in my html page.. i got a code which reads frm an array and stores in dropdown box..i mixed dat code with ur code which reads each line frm the file n stores in the array..bt i need that the same file is read everytime wich contains the name wich has to be displayed in combo box..
If u have any code plz send me..i need it urgently..
in brief
1) read content of the file
2) store the content into array
3) read the array n store the content into combobox in html.
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
var nesne ;
if(navigator.appName.search('Microsoft')>-1) { nesne = new ActiveXObject('MSXML2.XMLHTTP'); }
else { nesne = new XMLHttpRequest(); }
function yolla() {
nesne.open('get', 'bilgi.txt', true);
nesne.onreadystatechange= cevap;
nesne.send(null);
}
function cevap() {
if(nesne.readyState==4) {
var el = document.getElementById('bilgi');
el.innerHTML = nesne.responseText;
}
}
</script>
</head>
<body>
<input type="button" value="bilgi.txt dosyadaki yazıyı buraya al" onclick="yolla()">
<div id="bilgi"></div>
</body>
</html>
bilgi.txt
Code:
5
I use firefox 4.0b9
I clicked button, it is writing 5 in div
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
Bookmarks