Blog

Load text file in JavaScript

Posted by Ajay Patel Blogs | JavaScript 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>
COMMENT USING
PREMIUM SPONSORS
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
More Blogs from this Blogger
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.