Reading a file using javascript

Posted on 2003-11-06
Last Modified: 2008-02-01
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?
Question by:shlomi
  • 5
  • 4

Expert Comment

ID: 9697559
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.

LVL 11

Expert Comment

ID: 9699563
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?
LVL 11

Expert Comment

ID: 9699565
Or are you just wanting to print out part of another file in your HTML? A servserside include might also provide a solution.
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Author Comment

ID: 9702480
How come says it's possible with server side javascript.

Am i missing something?


Author Comment

ID: 9702489
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.
LVL 11

Expert Comment

ID: 9703292
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?

Author Comment

ID: 9703360
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?).

LVL 11

Accepted Solution

Zontar earned 100 total points
ID: 9703567
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.

Author Comment

ID: 9703605
Thanks a lot,
You have clearly made is simpler for me.
LVL 11

Expert Comment

ID: 9704128
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tools for analysis of code 4 49
Can't get page to reload 9 18
what should i learn? javascript or typescript? 3 38
How do I split a variable to newline 2 18
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question