How to read local file and echo to innerHTML?

I want a function that reads a local (server side) file and then drops the content into a div on the main html page.  Something isn't working, and I'm not sure what.  The code is here, the file 'itworks.html' just has the words 'It works!'

Thanks.
<html>
<head>
</head>
</body>
<script language='javascript'>
function readAndEcho(pat) 
{
	fh = fopen(pat, 0); 		// Open the file for reading
	if(fh!=-1) 							// If the file has been successfully opened
	{
	    length = flength(fh);         	// Get the length of the file    
	    str = fread(fh, length);     	// Read in the entire file
	    fclose(fh);                    	// Close the file
	    
	    // Return the contents of the file    
		return str;
	}
}
</script>
<a href="javascript:void(0)" onClick="document.getElementById('content').innerHTML=readAndEcho('itworks.html')">Click</a>
 
<div id='content'></div>
 
</body>
</html>

Open in new window

Wolfie7873Asked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
try:
<html>
<head>
</head>
</body>
<script language='javascript'>
function getObj()
{
	try{return new XMLHttpRequest();}catch(e){}
	try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}
	try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
return null;
}
function readAndEcho(pat) 
{
	var http=null;
	if( http=getObj() )
	{
		http.open("GET",pat,false);
		if( http.status==200 )
			return http.responseText;
		else if( http.status==404 )
			return "File does not exist";
		else
			return "HTTP Code: " + http.status;
	}
return "";
}
</script>
<a href="javascript:void(0)" onClick="document.getElementById('content').innerHTML=readAndEcho('itworks.html')">Click</a>
 
<div id='content'></div>
 
</body>
</html>

Open in new window

0
 
hieloCommented:
you need "send" the request:
...
            http.open("GET",pat,false);
            http.send(null);
            if( http.status==200 )
...
0
 
Wolfie7873Author Commented:
I don't get what I expected from this. I get

HTTP Code: 0

0
 
Wolfie7873Author Commented:
My error was a server one, not a code one.  Thanks!
0
 
hieloCommented:
>>My error was a server one, not a code one
So, why the grade?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.