Jump to content

[SOLVED] Reading Files


schme16

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.