Jump to content

Text files in Javascript


Elnof

Recommended Posts

I've been doing javascript for a while, but I've never had to do this before. So, here's my question: Is it possible to get the text from a file and put it on the web page using javascript?Thanks in advance!

Link to comment
Share on other sites

Ajax is the only way if the text file is on the server. This requires some server side scripting as well (like PHP or ASP.Net).It is also possible, but not very practical, to access a file on the client's PC providing the user has given JS the proper permissions (don't count on it :))

Link to comment
Share on other sites

Ajax is the only way if the text file is on the server. This requires some server side scripting as well (like PHP or ASP.Net).It is also possible, but not very practical, to access a file on the client's PC providing the user has given JS the proper permissions (don't count on it :))
AJAX can read the text file directly without the aid of any server-side language, like this:xmlHttp.open("GET","file.txt",true);And the responseText will have the contents of the text file.
Link to comment
Share on other sites

AJAX can read the text file directly without the aid of any server-side language, like this:xmlHttp.open("GET","file.txt",true);And the responseText will have the contents of the text file.
biggrin.gifNeat trick. I did not know that. Thanks.
Link to comment
Share on other sites

Of course, you have to create an instance of the XMLHttpRequest object before that and store it to the xmlHttp var. Also, you have to xmlHttp.send(null); to send yoru request.

Link to comment
Share on other sites

Ok, I've got my ajax, is there anyway to do it using local drives? This is going to be stored on my computer, and I'm getting a javascript error telling me that my script doesn't have permission to acess the file I want to read. Here's my scipt

function getFile(pURL) {    pFunc = 'repairText';    document.getElementById('mainText').innerHTML = "<p>Loading Text...</p>";    if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc         xmlhttp=new XMLHttpRequest();        eval('xmlhttp.onreadystatechange='+pFunc+';');        xmlhttp.open("GET", pURL, true); // leave true for Gecko        xmlhttp.send(null);    } else if (window.ActiveXObject) { //IE         xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');         if (xmlhttp) {            eval('xmlhttp.onreadystatechange='+pFunc+';');            xmlhttp.open('GET', pURL, false);            xmlhttp.send();        }    }}function repairText() {   var text;   var ready = 0;   var text2;       if (xmlhttp.readyState==4) {         if (xmlhttp.status==200) {             text=xmlhttp.responseText;            ready = 1;        }    }        if(ready == 1) {        text2 = text.replace(/~p~/gi, "<p>")        text = text2.replace(/~!p~/gi, "</p>");        text2 = text.replace(/~img~/gi, "<img src=\"");        text = text2.replace(/~!img~/gi, "\" />");        text2 = text.replace(/~sup~/gi, "<div class=\"sup\">");        text = text2.replace(/~!sup~/gi, "</div>");        text2 = text.replace(/~n~/gi, "<br />");        text = text2.replace(/~l~/gi, "<hr class=\"textLine\" />");                placeText(text);    }}function placeText(finText) {   document.getElementById('mainText').innerHTML=finText;}

Link to comment
Share on other sites

AJAX can read the text file directly without the aid of any server-side language, like this:xmlHttp.open("GET","file.txt",true);And the responseText will have the contents of the text file.
I didn't realize that either. Thanks.I think you have to use ActiveX http://4umi.com/web/javascript/fileread.htm
Link to comment
Share on other sites

Yep, most unfortunately using standard browser controls you cannot access the local filesystem, due to security concerns. Also unfortunately, perhaps, Microsoft provides a way round this, which along with other concessions is why IE is often considered a very insecure browser.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...