www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: how to use javascript in html to read txt file and display it ?

Hybrid View

  1. #1
    Join Date
    Jun 2012
    Posts
    5

    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.

  2. #2
    Join Date
    Jun 2012
    Posts
    5
    the local file is in server,not client computer,pls attenion

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Exclamation

    Quote Originally Posted by cxv5y745 View Post
    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.

  4. #4
    Join Date
    Jun 2012
    Posts
    5
    ok,thanks for reply,my attemp is the following,i get nothing in browser,pls comment,thanks.
    ----my code---
    <html>
    <head>
    <title>Example 02</title>

    <script type="text/javascript">
    file = fopen(getScriptPath("Contact.txt"), 0);.
    file_length = flength(file);.
    content = fread(file, file_length);.
    document.getElementById("myDiv").innerText = content;.
    </script>

    </head>
    <body>

    <div id="myDiv"></div>

    </body>
    </html>
    -------------------------

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355
    'fopen' and 'fread' are not JS commands.

    Looks like you might be mixing PHP with JS.

    Google 'javascript fopen' or 'javascript fread' for additional information.

  6. #6
    Join Date
    Jun 2012
    Posts
    5
    I google "javascript fopen" and see this link
    http://www.careerride.com/JScript-re...rite-file.aspx
    it look fopen and fread is js command from the link,and is it?

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    This page is relative to the reading of javascript files with Java Scripts ! See for example this page to use Ajax calls.
    Last edited by 007Julien; 06-22-2012 at 12:01 PM.

  8. #8
    Join Date
    Jun 2012
    Posts
    5
    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.

  9. #9
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Exclamation

    Quote Originally Posted by cxv5y745 View Post
    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?

  10. #10
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    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>
    tested in ff, chrome, and ie

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Thumbs up

    Quote Originally Posted by rnd me View Post
    don't listen to people who don't know how to do things...
    Code:
    ...
    tested in ff, chrome, and ie
    Another reason to upgrade browser to HTML5 I guess.

  12. #12
    Join Date
    Mar 2013
    Posts
    6
    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,

  13. #13
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by rahul1990 View Post
    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.

  14. #14
    Join Date
    Mar 2013
    Posts
    6
    hi rnd me,

    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.

    Thanks

  15. #15
    Join Date
    Sep 2007
    Posts
    315
    yolla.htm
    Code:
      
    <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
    Bismillahirrahmanirrahîm
    Hamd, Âlemlerin Rabbi, Rahmân, Rahîm, hesap ve ceza gününün (ahiret gününün) maliki Allah'a mahsustur. (Allahım!) Yalnız sana ibadet ederiz ve yalnız senden yardım dileriz. Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles