...

>> Reading text file using JavaScript

premshree
06-24-2002, 06:16 AM
Somebody had posted this query on how to read text files using JavaScript.......can't find that post now...

Anyways, though it's not possible to read text-files using JavaScript it is possible to read XML files using it.

Here is an XML based JavaScript Ticker

http://www.dynamicdrive.com/dynamicindex2/xmlticker.htm

It works only on browsers supporting XML (IE)

Enjoy! :)

glenngv
06-24-2002, 06:28 AM
go to http://www.devguru.com for some tutorials on XML

nolachrymose
06-24-2002, 04:19 PM
You can use behaviors. Here's an example that I've posted here "for your viewing pleasure" :rolleyes::

<html xmlns:behave_yaself>
<head>
<title>Behaviors</title>
<script type="text/javascript">
function downloadEnd(e) {
document.all.download.value=e;
}
</script>
<style type="text/css">
textarea {
background-color: #000;
color: #FFF;
scrollbar-face-color: #FFF;
}
</style>
</head>
<body>
<behave_yaself:download id="currFile" style="behavior:url(#default#download);" />
Click <a href="javascript:document.getElementById('currFile').startDownload(location.href,downloadEnd)">here</a>
to start the download.
<br />When the download is finished, the text will be displayed in the <b>TEXTAREA</b> below.
<p><textarea rows="20" cols="40" id="download"></textarea></p>
</body>
</html>

Hope that helps!

Happy coding! :)

fuchka
07-04-2005, 01:15 PM
If the above javascript is used in a asp.net web form, it will show the contents of the aspx file. But can you tell me if the above javascript can be modified to read any text file in the client machine? I tried changing it to :
javascript:document.getElementById('currFile').startDownload('D:\ReadFile.txt',downloadEnd)"
But it is throwing "permission denied" javascript error. Any workaround?

rlemon
05-18-2006, 06:21 PM
Somebody had posted this query on how to read text files using JavaScript.......can't find that post now...

Anyways, though it's not possible to read text-files using JavaScript it is possible to read XML files using it.

Here is an XML based JavaScript Ticker

http://www.dynamicdrive.com/dynamicindex2/xmlticker.htm

It works only on browsers supporting XML (IE)

Enjoy! :)

Sorry but that last note about reading text files is not possible in JS is incorrect.

It is possible, however the user has to accept an activeX component.

you can parse the users FSO via js with almost the exact same functions you use in ASP

Look up the JS Drive object and File objects

KC-Luck
05-18-2006, 07:20 PM
No HTTP served page should access your local harddrive unless you allow that to happen, and as mentioned that would require allowing either the Scripting FileSystemObject within MS IE, or by setting browser properties to allow such a transaction to happen via XMLHttpRequest objects.

rlemon
05-30-2006, 03:40 PM
which can be done with signed scripts (RE: Allowing the FSO access)

rlemon
05-30-2006, 03:43 PM
one example:
http://rlemon.com/rltp/

note, there are limitations to this code (but can be worked out) - also, there is very little documentation on the JS syntax.

vwphillips
05-30-2006, 05:32 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
<!--
var srcFrame;
//External content into a layer
function loadOuter(doc) {
srcFrame = document.getElementById("hiddenContent");
srcFrame.src = doc;
// workaround for missing onLoad event in IFRAME for NN6
if (!srcFrame.onload) {
setTimeout("transferHTML()", 1000)
}
}

function transferHTML(){
srcContent='';
if (srcFrame.contentDocument){
srcContent=srcFrame.contentDocument.getElementsByTagName("BODY")[0].innerHTML;
}
else if (srcFrame.contentWindow){
srcContent=srcFrame.contentWindow.document.body.innerHTML;
}
document.getElementById("outerDisplay").innerHTML = srcContent
}


var DocAry=new Array('Test.txt','Test2.txt');

function SelectList(v){
if (v>0){
loadOuter(DocAry[v-1]);
}
}

//-->
</script>

<INPUT TYPE="button" VALUE="Test.txt" onClick="loadOuter('Test.txt')" >

<INPUT TYPE="button" VALUE="Test2.txt" onClick="loadOuter('Test2.txt')" >
<br>

<select name="fred" size="1" onchange="SelectList(this.selectedIndex);">
<option >Select a List</option>
<option >List 1</option>
<option >List 2</option>
</select>

<div id="outerDisplay"></div>

<iframe id="hiddenContent" width="200" height="200" style="position:absolute;visibility:hidden;" ></iframe>

</body>

</html>



Test.txt
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7



Test2.txt
Item 11
Item 12
Item 13
Item 14
Item 15
Item 16
Item 17

kirtley
05-14-2009, 10:37 AM
That's a great solution! is it possible to access the text in the iframe? I mean, can I transfer each line to a variable?

rnd me
05-14-2009, 12:14 PM
That's a great solution! is it possible to access the text in the iframe? I mean, can I transfer each line to a variable?

Your'e new, but it's normally bad form to hop on old posts.

I would (shamelessly) recommend you check out this more recent post about loading and saving files with javascript (http://codingforums.com/showthread.php?t=143412&highlight=file).

using it, if you wanted and array of the lines in myfile.txt, you could say:
var lines=IO("myFile.txt").split(/\r?\n/g);

adarshyam
05-27-2010, 09:11 PM
hi, can you please give me a better solution for

if (!srcFrame.onload) {
setTimeout("transferHTML()", 10);
}
else{
setTimeout("transferHTML()", 10);
}

because, it works for IE and FF but not for Google chrome. Thank you

Philip M
05-27-2010, 09:37 PM
adarshyam - Please do not hijack someone else's (old) thread. Start a new thread of your own, paying attention to the forum Rules and Guidelines before you post.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum