Blog

Load text file in JavaScript

Posted by Ajay Patel Blogs | JavaScript, CSS Jun 02, 2012
Here is simple example for reading a text file using JavaScript.  
Download Files: General.zip

We use XMLHttpRequest object to read/write data from server side.

This example based on that.

It is simple to understand.

Read Text file from JavaScript 

function read()
{

     var txtFile = new XMLHttpRequest();
     txtFile.open("GET", "http://localhost:9122/Text.txt", true);
     txtFile.onreadystatechange = function () 
     {
     if (txtFile.readyState === 4) 
    {  
        // Makes sure the document is ready to parse.
        if (txtFile.status === 200) 
        {  
             // Makes sure it's found the file.
             document.getElementById("div").innerHTML = txtFile.responseText;  
        }
    }
}
txtFile.send(null)
}

HTML 


<
body onload="read();">
    
<form id="form1" runat="server">
   
    <div id="div">
       
</div>
    
</form>
</body>
post comment
     

Hey, Its working Fine. may I have your code where you implement this ?

Posted by Ajay Patel May 13, 2013

it is giving error XMLHttpRequest is undefined

Posted by Mayur Gujrathi Jun 07, 2012

it is giving error XMLHttpRequest is undefined

Posted by Mayur Gujrathi Jun 07, 2012

it is giving error XMLHttpRequest is undefined

Posted by Mayur Gujrathi Jun 07, 2012

it is giving error XMLHttpRequest is undefined

Posted by Mayur Gujrathi Jun 07, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
More Blogs from this Blogger
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter