Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

How can i read a *.txt file in javascript

i want to read some javascript inputs from a text file for my HTML file.
0
renjitkumar
Asked:
renjitkumar
  • 3
  • 2
2 Solutions
 
Tomarse111Commented:
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
 
renjitkumarAuthor Commented:
i tried this option. but i have proble to refer the file path. because will available in desktop only not in HTML section.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
renjitkumarAuthor Commented:
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
 
GawaiCommented:
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
 
GawaiCommented:
did u know u can split the points ? Tomarse111 deserve as well
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now