Solved

How can i read a *.txt file in javascript

Posted on 2009-04-01
8
476 Views
Last Modified: 2012-05-06
i want to read some javascript inputs from a text file for my HTML file.
0
Comment
Question by:renjitkumar
  • 3
  • 2
8 Comments
 
LVL 20

Expert Comment

by:Gawai
ID: 24038527
0
 
LVL 15

Assisted Solution

by:Tomarse111
Tomarse111 earned 500 total points
ID: 24038655
Without any additional addons, this can only be done through AJAX. Please see below for an example ... where the test.txt is in the same dir as the file you're working on.
<html>

<head>

<script>

function submitForm()

{ 

    var xhr; 

    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }

    catch (e) 

    {

        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }

        catch (e2) 

        {

          try {  xhr = new XMLHttpRequest();     }

          catch (e3) {  xhr = false;   }

        }

     }

  

    xhr.onreadystatechange  = function()

    { 

	

         if(xhr.readyState==4)

         {

              	if(xhr.status  == 200){ 

                  document.write("Received:"  + xhr.responseText); 

				  return false;

				}

         }

    }; 
 

   xhr.open('GET', "test.txt",  true); 

   xhr.send(null); 

} 

</script>

</head>

                 

<body>

    <FORM method="POST" name="ajax" action="">                  

         <INPUT type="BUTTON" value="Submit"  ONCLICK="submitForm()">

        

    </FORM>

 </body>

</html>

Open in new window

0
 

Author Comment

by:renjitkumar
ID: 24047985
i tried this option. but i have proble to refer the file path. because will available in desktop only not in HTML section.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Accepted Solution

by:
renjitkumar earned 0 total points
ID: 24049578
i foud the solution myself; below is the source code of my query

<html>
<head>
<script language="JavaScript" type="text/javascript">
var text;

function fetchText(txtFile) {
var d = document;
var txtFrame = d.getElementById( 'textReader');
txtFrame.src = txtFile;
setTimeout('text = getContent();', 500);
}

function getContent() {
var d = document;
var txtFrame = d.getElementById( 'textReader');
var text = '';
if (txtFrame.contentDocument) {
var d = txtFrame.contentDocument;
text = d.getElementsByTagName( 'BODY')[ 0].innerHTML;

}
else if (txtFrame.contentWindow) {
var w = txtFrame.contentWindow;
text = w.document.body.innerHTML;

}
return text;
}

function doTest(txtFile, searchFor) {
fetchText(txtFile);
setTimeout("alert(-1 != text.indexOf('" + searchFor + "'))", 750);
}
</script>
</head>
<body>
<iframe id="textReader" style="display: none;" ></iframe>
<form>
<input type="button" value="search test 1 - will pass" onclick="doTest('C:/test.txt', 'HTML page')" />
</form>
</body>
</html>
0
 
LVL 20

Expert Comment

by:Gawai
ID: 24051114
go ahead for that, next time when you will ask any question, experts will think twice before they post their solution, i guess we have given you proper direction for the solution. Enjoy !
0
 
LVL 20

Expert Comment

by:Gawai
ID: 24074464
did u know u can split the points ? Tomarse111 deserve as well
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now