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
- function read()
- {
- var txtFile = new XMLHttpRequest();
- txtFile.open("GET", "http://localhost:9122/Text.txt", true);
- txtFile.onreadystatechange = function()
- {
- if (txtFile.readyState === 4)
- {
-
- if (txtFile.status === 200)
- {
-
- document.getElementById("div").innerHTML = txtFile.responseText;
- }
- }
- }
- txtFile.send(null)
- }
HTML
- <body onload="read();">
- <form id="form1" runat="server">
- <div id="div">
- </div>
- </form>
- </body>