Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Reading a file using javascript

Posted on 2003-11-06
Medium Priority
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 (x.open("r"))
      while (!x.eof())
      {            line = x.readln()

But it doesn't work, Can anyone realize why?
Question by:shlomi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 9702480
How come http://docs.sun.com/source/816-6410-10/file.htm 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 400 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 www.activestate.com) 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

636 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