Load text file in JavaScript

Introduction 

 
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
  1. function read()  
  2. {  
  3.      var txtFile = new XMLHttpRequest();  
  4.      txtFile.open("GET""http://localhost:9122/Text.txt"true);  
  5.      txtFile.onreadystatechange = function()   
  6.      {  
  7.           if (txtFile.readyState === 4)   
  8.           {  
  9.                // Makes sure the document is ready to parse.  
  10.                if (txtFile.status === 200)   
  11.                {  
  12.                     // Makes sure it's found the file.  
  13.                     document.getElementById("div").innerHTML = txtFile.responseText;  
  14.                }  
  15.           }  
  16.      }  
  17.      txtFile.send(null)  
  18. }  
HTML
  1. <body onload="read();">  
  2.     <form id="form1" runat="server">  
  3.        <div id="div">  
  4.        </div>  
  5.     </form>  
  6. </body>