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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.