How do I read a server-side text file into JavaScript variables ?

M

Martin G

I have a text file, with all my data (text and numbers),
stored in the same directory on the web as my HTML/JavaScript files;

How do I open the text file,
and read the data into my JavaScript variables ?

Then presumably I have to close the file.

I do not need to write to files.

If I can't do this using HTML/JavaScript,
then what is the easiest method ?
 
M

MikeR

Here's an example using ASP (VBScript) I found by Googling "using fso". FSO is Microsoft's
File System Object, if your host supports ASP.
Sorry, I can't help with javascript.
MikeR 1st
 
S

Steve Easton

Why does the data for the variables have to be in an external file??
If your variables need to be global, simply declare them right after the opening script
tag, and "outside" of any functions.


--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer
 
J

James Clark

If you want to use asp then there is a file system object you can use.
I'm posting a script that reads each line of a text file and then
stores it inside a server-side javascript variable. You can make some
pretty simple modifications to this script to store the text lines
inside a client side javascript variable.



<%@LANGUAGE="JavaScript"%>
<HTML>
<BODY>
<%
var ASPScriptObject =
Server.CreateObject("Scripting.FileSystemObject");
var myPath=Server.MapPath("\\") + "\\folder_name\\textFileName.txt"
var AspScript = ASPScriptObject.OpenTextFile(myPath);
var outputScript="";

while(!AspScript.AtEndOfStream)
{
outputScript += AspScript.ReadLine() + "\r";
}

outputScript = new String(outputScript);
outputScript=Server.HTMLEncode(outputScript)
AspScript.Close();
outputScript = "<PRE>" + outputScript + "</PRE>";
Response.Write(outputScript)
%>
</BODY>
</HTML>
 
M

MikeR

I sure wish someone had given an answer. I'm with Crash, I'd like to know too.
MikeR 1st
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top