One of the easiest ways is to ask the server to return a pre-filled text area (Here is an example of using PHP):
<textarea name="text" rows="20" cols="70"> <?php echo file_get_contents('yourFile.txt'); ?> </textarea>
Note. Something similar can be done with any server-side scripting language .
In the meantime, if you need to load it dynamically, it is best to use AJAX . Choose which approach is best for coding and support. While jQuery is a popular approach, you can use whatever is convenient for you, and you might first want to learn about XmlHttpRequest .
AJAX dynamic requests with Pure JavaScript can be tricky, so make sure your solution is cross-browser. A common mistake is to use XmlHtpRequest directly and the inability to make it compatible with older versions of IE, which leads to random errors depending on which browser / version you are using. For example, it might look like this (you will need to test it on all target browsers to add backups if necessary):
Pure JS:
if (typeof XMLHttpRequest === "undefined") { XMLHttpRequest = function () { try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {} try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} throw new Error("This browser does not support XMLHttpRequest."); }; } function readBOX() { function reqListener () { document.forms[0].text.value = this.responseText; } var txtinput = document.getElementById("txtinput").value; var filePath = "http://mywebsite.com/textfile/" + txtinput + ".txt"; var oReq = new XMLHttpRequest(); oReq.onload = reqListener; oReq.open("get", filePath, true); oReq.send(); }
But if you don't mind sacrificing some actions in order to provide maximum support, you should use jQuery implementation:
JQuery
function readBOX() { var txtinput = document.getElementById("txtinput").value; var filePath = "http://mywebsite.com/textfile/" + txtinput + ".txt"; $.ajax({ url: filePath }).done(function(data){ document.forms[0].text.value = data; }); }
Note. The jQuery library is very large, but keep in mind that if you enable it directly from google servers, your user most likely already has it in the cache.
Hope this helps :)
Frederik.L
source share