Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to read local file and echo to innerHTML?

Posted on 2008-11-11
5
Medium Priority
?
2,090 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Wolfie7873
  • 3
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 150 total points
ID: 22932100
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
 
LVL 82

Expert Comment

by:hielo
ID: 22932113
you need "send" the request:
...
            http.open("GET",pat,false);
            http.send(null);
            if( http.status==200 )
...
0
 

Author Comment

by:Wolfie7873
ID: 22932353
I don't get what I expected from this. I get

HTTP Code: 0

0
 

Author Closing Comment

by:Wolfie7873
ID: 31515585
My error was a server one, not a code one.  Thanks!
0
 
LVL 82

Expert Comment

by:hielo
ID: 22933124
>>My error was a server one, not a code one
So, why the grade?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question