schme16 Posted May 3, 2007 Share Posted May 3, 2007 i've scoured the net and all i can find is ways to read XML data, but people keep saying its easy to get the contents of a txt file (the file is on the server that the script will be launched from...i was informed that it cannot be done otherwise) but no-one will sya how its done.... Thanks in advance for any help! Quote Link to comment Share on other sites More sharing options...
veridicus Posted May 3, 2007 Share Posted May 3, 2007 Just as you request an XML file from the server with JavaScript, you can also request a text file. Technically you can request anything the server is willing to return. What JS gives you is a built-in XML parser. But you can use string functions, regex, etc, to get info out of anything, including a txt file. Quote Link to comment Share on other sites More sharing options...
nogray Posted May 3, 2007 Share Posted May 3, 2007 I think the easiest way to read a text file through iframe. Just call the text file in a hidden iframe and read the content using a script. Here is a quick example <script language="javascript"> var did_load=false; function grap_cont(){ if (did_load){ var frm = document.getElementById('hidden_frame').contentWindow.document; var txt = frm.body.innerHTML.replace(/<[^>]*>/g,""); alert(txt); } } </script> <a href="txt1.txt" target="hidden_frame">First text file</a><br /> <a href="txt2.txt" target="hidden_frame">Second text file</a> <iframe src="blank.html" name="hidden_frame" id="hidden_frame" onload="grap_cont(); did_load=true;" style="width:0px; height:0px;" frameborder="0" scrolling="no"></iframe> Quote Link to comment Share on other sites More sharing options...
schme16 Posted May 9, 2007 Author Share Posted May 9, 2007 Thanks guys much help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.