Reading a file using javascript

I am trying to read a file from the web server using javascript.

I know client side javascript can't read a file so what i'm trying is to have the web page activate a javascript on the server.

The web page looks like:


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-8">

<BODY bgcolor=66FFFF bgproperties="fixed">


<script language="javascript" src="javascript.js"></script>


On the web server i have this file


x = new File("c:\temp\userInfo.txt")
if ("r"))
      while (!x.eof())
      {            line = x.readln()

But it doesn't work, Can anyone realize why?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

To answer your question i one word, No, you can't read anything on the server (except .js files) with javascript.  You also can't have javascript activate on the server.  MS Frontpage has server side functions, but I personally don't trust them.  The best option would be to use PHP to read the file and out put each line induvidually.  If you need a PHP script to do that, I would be ahppy to provide one.

You can do this using PHP or ASP, and I suspect most if not all other serverside programming languages.

What sort of info is in the file, and how do you intend to use this info clientside?
Or are you just wanting to print out part of another file in your HTML? A servserside include might also provide a solution.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

shlomiAuthor Commented:
How come says it's possible with server side javascript.

Am i missing something?

shlomiAuthor Commented:
Here is what i'm trying to do:

I want to have a marquee that includes text coming from this file.
First i have to read the file into an array, than i have to display each variable of the array.
Serverside JavaScript and clientside JavaScript have absolutely nothing to do with each other, except that the core language is the same -- the programming environments and available objects are completely different. Clientside JavaScript has no access to the filesystem, except you can include .js files -- but these must consist of clientside code.

What's running on your server? If you have ASP, you can do this -- read a file one line at a time, write a clientside JS array declaration in which each line from the file is written as an element in the array. The important thing to note is that we use Response.ContentType to send a "Content-Type: text/javascript" header to the browser before actually writing the output so the browser will in fact interpret the text as JavaScript and not plaintext.

Source of marquee-txt.asp:

  Set FSO = Server.CreateObject("Scripting.FileSystemObject");
  Set File = FSO.GetFile("C:\TEMP\userInfo.txt")
  File.OpenAsTextStream(1, 0)

  Output = "var myArray = ["

  Do While Not File.AtEndOfStream
    Line = File.ReadLine
    Output = """" & Line & """, " & Output

  Output = Left(Output, Len(Output) - 1)
  Output = "];" & Output

  Response.ContentType = "text/javascript"
  Response.Write Output

Now in your HTML file, just use

<script type="text/javascript" src="marquee-txt.asp"></script>

and the browser will treat the output of this ASP file (not the ASP code, but the text generated by the ASP code) as though it were the content of a .js file.

As I said, the same thing can be done with PHP. What do actually you have running on your server?
shlomiAuthor Commented:
Thanks for the answer, However i think i am a little mixed up.
I understand that Javascript has two braches: server and client.

And i know that the server side language can handle readina  local file on the server.

Why is it that The script you wrote is VB?
Also, In regarding to ASP: Is it a language or just a mechanisem to produce dynamic content ?

I am running Apache (Does it support VB, Javascript?).

ASP is a programming environment. It supports both VBScript and JScript "out of the box" and other scripting languages (Perl and Python among themsee can be installed to work with it. Most ASP programmers use VBScript, but I much prefer JScript (I'm better with JS, and it's a more flexible/powerful language IMO). I used VBScript in my example so as not to add to the confusion.

ASP/ JScript version of the above:

  var fso = Server.createObject("Scripting.FileSystemObject");
  var file = fso.getFile("C:\TEMP\userInfo.txt");
  file.openAsTextStream(1, 0);

  var output = "var myArray = [";
  var line;

    line = file.readLine();
    output += "\"" + line + "\", ";


  output = output.slice(0, -1) + "];";

  Response.contentType = "text/javascript";

Apache doesn't support ASP, only MS-IIS does that (unless you want to buy a fairly expensive ASP clone from Sun). If you're running Apache, my suggestion would be to install PHP. It's free, a fairly easy install, it's a heck of a lot easier to learn quickly than either Perl or Python, and works great on Windows (I have two Windows servers running here, one using IIS and one using Apache, both running PHP). To use SunONE SSJS, you'd have to install their server -- I know Netscape used to have a free developer verison, but I never installed it myself as it was a pretty big download.

PHP version:

  $fp = fopen("C:\\TEMP\\userInfo.txt");

  $output = "var myArray = [";

  while( !feof($fp) )
    $line = fgets($fp, 1024);
    $output .= "\"$line\"',";


  $output = substr($line, 0, -1) . "];"

  header("Content-Type: text/javascript");
  echo $output;

It's possible there might be a glitch or two in my examples -- I wrote them all off the top of my head -- but the same basic concepts apply in each, and with a little tweaking, any of them should work in the right environment.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shlomiAuthor Commented:
Thanks a lot,
You have clearly made is simpler for me.
No worries.

I started using this technique in PHP a while back to generate JS and CSS files from info stored in a MySQL DB.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.