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 ?

  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,373

    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,373
    '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,266
    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,373

    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,373

    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
    Sep 2007
    Location
    istanbul
    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.

  13. #13
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    If all the writers had the honesty to say that they use HTLML5 and that their scripts work only on the most recent browsers, we avoid many confusions... The HTML5 proposed solution work only with windows7 (Internet explorer 9 can not be settled on older system).

    A solution supported by more browsers an system consists, as suggests Ayse, in using Ajax. Here is an other example (See the source) build with the page XHTMLRequest functions from Peter-Paul Koch...
    Last edited by 007Julien; 06-23-2012 at 05:38 AM.

  14. #14
    Join Date
    Mar 2013
    Posts
    1

    Cool Inputting address into code

    I am using the code that 'rnd me' submitted, but am having issues hard inputting the address into it. I'm building a situational awareness page at work that runs off of our local drive. Currently we just iframe a few .txt files from the Space Weather Prediction Center, but I would like to readastext them and write them so I could modify the text with css. I do not need to use the input=file as the address never changes, but I'm having a heck of time getting to work... any thoughts?

    Code:
    <html>
    <head>
    <title>SWPC Events</title>
    
     <script type="text/javascript">
    
    		
    		var reader = new FileReader();
    
    		function readText(){
    
    		
    				var reader = new FileReader();
    				
    				reader.onload = function (e) {  
    					var output=e.target.result;
    					document.getElementById('main').innerHTML= output;
                                                                 }
    				        reader.readAsText('http://www.swpc.noaa.gov/ftpdir/indices/events/events.txt');
    
    					
    		                    } 
    
    </script>
    
    </head>
    <body>
    	<input type="button" id="decode" onclick="readText()" value="Decode">
    	<div id="main"></div>
      </body>
    </html>

  15. #15
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by 007Julien View Post
    If all the writers had the honesty to say that they use HTLML5 and that their scripts work only on the most recent browsers, we avoid many confusions... The HTML5 proposed solution work only with windows7 (Internet explorer 9 can not be settled on older system).
    not true.
    firefox and chrome run on windows XP-win8 and mac and mobile.
    IE9 only needs windows Vista, not win7.

    all told, HTML is supported on about 9/10 devices, and that final ten percent should be converted over the next couple of years to about 2%.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 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