Blog : Reading from a txt file to JavaScript -html
Reading from a txt file to JavaScript -html
Discuss Reading from a txt file to JavaScript -html in the JavaScript Development forum on Dev Shed.
Reading from a txt file to JavaScript -html JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
Reply
Add This Thread To:
Del.icio.us Digg Google Spurl Blink Furl Simpy Y! MyWeb
« Previous Thread | Next Thread »
Thread Tools Search this Thread Rate Thread Display Modes
Unread Dev Shed Forums Sponsor:
The Shed is going Social! We have officially launched our new Facebook page and we need your help to make our community bigger than ever! Start by liking our Fan page, then hop in on the conversation. Join Now! The Shed is going Social! We have officially launched our new Facebook page and we need your help to make our community bigger than ever! Start by liking our Fan page, then hop in on the conversation. Join Now!
#1
Old June 28th, 2007, 01:23 PM
victoria++ victoria++ is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
Join Date: Sep 2005
Posts: 52 victoria++ User rank is Just a Lowly Private (1 - 20 Reputation Level)
Time spent in forums: 12 h 42 m 26 sec
Reputation Power: 8
Reading from a txt file to JavaScript -html
Hi, I am trying to read from a txt file, line by line and added to an array which then will be seeded with random numbers to use random lines to show on html-js file. I have the random function (number creator) and the array. My issue is reading from a txt file line by line and loading into an array (all of this using JS) thanks.
Reply With Quote
#2
Old June 28th, 2007, 02:33 PM
draelon draelon is offline
Dissident
Dev Shed Intermediate (1500 - 1999 posts)
Join Date: Mar 2003
Location: New York
Posts: 1,671 draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)
Time spent in forums: 1 Day 8 h 8 m 13 sec
Reputation Power: 47
First off, this can only be done if the javascript is being served from the same computer that has the txt file. This has to do with the javascript security model which prevents access to files.
That being said, here's a function:
javascript Code:
Original - javascript Code
// =====================================================================
// array_file_read - convert a file to an array
// =====================================================================
function array_file_read( inPath )
{
var fso, FILE;
var ForReading = 1;
$contents = new Array();
fso = new ActiveXObject("Scripting.FileSystemObject");
FILE = fso.OpenTextFile( inPath, ForReading, true );
while( ! FILE.AtEndOfStream )
{
$line = FILE.ReadLine();
if( $line.indexOf( '#' ) != 0 )
{
$contents[ $contents.length ] = $line;
}
}
FILE.Close();
return $contents;
}
I should note that this function will interpret the # character at the beginning of a string as a commented line and will skip that line.
__________________
Draelon
PHP Manual :: MySQL Manual :: How to Ask Questions the Smart Way
=======================================================
Reply With Quote
#3
Old June 28th, 2007, 02:36 PM
sumityadav's Avatar
sumityadav sumityadav is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
Join Date: May 2006
Location: Chandigarh, India
Posts: 311 sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level)sumityadav User rank is Major (30000 - 40000 Reputation Level) Folding Points: 37512 Folding Title: Starter FolderFolding Points: 37512 Folding Title: Starter Folder
Time spent in forums: 6 Days 12 h 9 m 50 sec
Reputation Power: 364
Send a message via MSN to sumityadav Send a message via Yahoo to sumityadav Send a message via Google Talk to sumityadav Send a message via Skype to sumityadav
MySpace Orkut
I searched for the same and found this. May be this would be helpful. The URL to the same is http://www.javascripter.net/faq/reading2.htm
The function below will read the file and set the value of the textarea to the file content.
javascript Code:
Original - javascript Code
var fileContent='';
var theLocation='';
function readFileViaApplet(n) {
document.f1.t1.value='Reading in progress...';
document.ReadURL.readFile(theLocation);
setTimeout("showFileContent()",100);
}
function showFileContent() {
if (document.ReadURL.finished==0) {
setTimeout("showFileContent()",100);
return;
}
fileContent=document.ReadURL.fileContent;
document.form1.textarea1.value=fileContent;
}
Quote:
Originally Posted by victoria++
Hi, I am trying to read from a txt file, line by line and added to an array which then will be seeded with random numbers to use random lines to show on html-js file. I have the random function (number creator) and the array. My issue is reading from a txt file line by line and loading into an array (all of this using JS) thanks.