/    Sign up×
Community /Pin to ProfileBookmark

reading from a serverside text file

Hi,

I am writing a javascript that needs to read data from a server-side text file to get information.

I know I can do this using server side scripting (php or something), but is there a way for a javascript to read this in and manipulate data without having to do anything on the server?

thanks for your help,
–andrew

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisJul 12.2006 — Javascript has no file handling capabilities. Also there are security measures that will prevent a user's computer from getting direct access to a file on the server.

You could load it into an iframe with "visibility: hidden" and then use innerHTML to get the text.

You could also restructure the text to be a simple script and include it in your page using the <script src="..."> tag.
Copy linkTweet thisAlerts:
@werdnaauthorJul 13.2006 — Thanks for your comment, Gil. Unfortunately, I couldn't get the IFrame working. I never figured out how to get access to the contained document. Instead, I turned the text file into a small script, as you suggest and I'm now using a multiline string:

<i>
</i>var info = ""+&lt;r&gt;&lt;![CDATA[

"Andrew's Garden" "49.26601230654396" "-123.12944412231445" "Me" "XXX-YYY-ZZZZ" "[email protected]" ""

]]&gt;&lt;/r&gt;;
Copy linkTweet thisAlerts:
@toicontienJul 13.2006 — Actually, it's usually window.[frame name].document.* if you want to access the document in another frame. But you need to give the frame a name using the name attribute.
<i>
</i>&lt;iframe name="foo"&gt;&lt;/iframe&gt;

A reference to it's document might be:
<i>
</i>var frameDoc = window.foo.document;
alert(frameDoc.innerHTML);
Copy linkTweet thisAlerts:
@toicontienJul 13.2006 — Here would be some working code:
[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>iframe test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- begin hiding
function loadStuff() {
var frameDoc = window.foo.document;
var person = eval("(" + frameDoc.body.innerHTML + ")");
person.yell();
}
// end hiding -->
</script>
</head>
<body onload="loadStuff();">
<iframe name="foo" src="json.html"></iframe>
</body>
</html>
[/code]

And the source for json.html:
[code=html]<html>
<head></head>
<body>
{
phrase: "AAAAHHH! A BUG CRAWLED INTO MY SCALP!!!",
yell: function() {
alert(this.phrase);
}
}
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@werdnaauthorJul 13.2006 — Thanks for the code. I'll try it out, but I don't know how well this will go over for the people I am writing this for. They want something simple that they can maintain (ie- make changes to the text file).

FTP is scary enough for them. I'd rather them not have to see any html at all. Using a database is also out of the question.
Copy linkTweet thisAlerts:
@toicontienJul 13.2006 — You can also do comma separated values. Leave the text file like I had it, and make sure they put their comma separate values in between the BODY tags.
[code=html]
<html> <head></head><body>
artist=The Music,album=Welcome To The North
artist=Clutch,album=Blast Tyrant
</body></html>
[/code]

Then do some string splitting to harvest the data.
Copy linkTweet thisAlerts:
@TheBearMayJul 13.2006 — Assuming the data is in the same domain as the page the following will place the file contents into fileData. Just a matter of parsing after that.

[code=php]
var fileData=getFile("pathToFileInSameDomain");

function getFile(fileName){
oxmlhttp = null;
try{
oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/xml");
}
catch(e){
try{
oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
return null;
}
}
if(!oxmlhttp) return null;
try{
oxmlhttp.open("GET",fileName,false);
oxmlhttp.send(null);
}
catch(e){
return null;
}
return oxmlhttp.responseText;
}
[/code]
Copy linkTweet thisAlerts:
@cutawayJul 22.2006 — I have inserted the code you wrote in a simple page. I want to get the output of http://isc.sans.org/infocon.txt which returns a single word (green, yellow, or red). I did modify the code to provide debugging information instead of null. In this case the returned value indicates that the webpage failed to load.

I've been banging my head on this for a while.

Thanks for any input,

Cutaway

[CODE]<HTML><HEAD>
<SCRIPT language="Javascript" type="text/javascript">
var fileData=getFile("http://isc.sans.org/infocon.txt");

function getFile(fileName){
oxmlhttp = null;
try{
oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/xml");
}
catch(e){
try{
oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
//return null;
return "failed MS<BR>";
}
}
if(!oxmlhttp) return "failed to create object<BR>";
try{
oxmlhttp.open("GET",fileName,false);
oxmlhttp.send(null);
}
catch(e){
return "failed to get webpage<BR>";
}
return oxmlhttp.responseText;
}

</SCRIPT>
<BODY>
Test Script:<br>
Output:
<script language="Javascript" type="text/javascript">
var valueFile = getFile();
if (valueFile == null){
document.write("null<BR>");
} else {
document.write( getFile() + ".<BR>" );
}
</script>
<br>END<br>
</BODY> </HTML>
[/CODE]
Copy linkTweet thisAlerts:
@TheBearMayJul 22.2006 — What does fileData contain? My guess is that the original call is loading the variable fileData, and the second/third call (in your output script) without a file name being designated is failing.
Copy linkTweet thisAlerts:
@cutawayJul 22.2006 — Opps, missed that one. Sorry. I just checked and they both failed. I used fileData and then I loaded put the url for the page in my var and neither worked.

The inforcon.txt file will contain one of the following words: green or yellow or red. It will not contain anything else.

Thanks for the quick reply,

Don
Copy linkTweet thisAlerts:
@TheBearMayJul 22.2006 — Hmmm.. Works for me. Assuming the html page is in the same directory as the text file try just using the file name.
Copy linkTweet thisAlerts:
@cutawayJul 22.2006 — Actually, the information is coming from a different webpage. I am actually doing this for a Yahoo! Widget. The widget will contact http://isc.sans.org/infocon.txt and get the status which is indicated by the word in the file.

Is this not the proper way to do this? What would you recommend?

Thanks,

Cutaway
Copy linkTweet thisAlerts:
@balloonbuffoonJul 22.2006 — No javascript solution (at least that I've come across) will be able to acess a file from another server and get any contents from it. Not even the iframe solution. (Its a security issue.) The only way would be to embed it with the <script> tag (like gil said), but then the file would have to be in javascript format-- declaring a variable or something. Look into the possiblities of server side scripts (like PHP), I'm not sure if its feasible for you, but its the best bet.

--Steve
Copy linkTweet thisAlerts:
@cutawayJul 22.2006 — Thank y'all. I guess I need to see what Yahoo! Widgets can do about importing the file and then parsing it locally.

Thanks again,

Cutaway
×

Success!

Help @werdna spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...