Solved

Reading a file using javascript

Posted on 2003-11-06
10
1,989 Views
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:

<HTML>

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

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


<br>

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



</BODY>
</HTML>

On the web server i have this file

javascript.js:

x = new File("c:\temp\userInfo.txt")
if (x.open("r"))
{
      while (!x.eof())
      {            line = x.readln()
            write(line+"<br>");
      }
x.close();
}

But it doesn't work, Can anyone realize why?
Thanks
0
Comment
Question by:shlomi
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:mattjp88
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.

-Matt
0
 
LVL 11

Expert Comment

by:Zontar
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?
0
 
LVL 11

Expert Comment

by:Zontar
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.
0
 

Author Comment

by:shlomi
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?

0
 

Author Comment

by:shlomi
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:Zontar
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:

<%@LANGUAGE="VBSCRIPT"%>
<%
  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
  Loop

  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?
0
 

Author Comment

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

Thanks
0
 
LVL 11

Accepted Solution

by:
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 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:

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

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

  while(!file.atEndOfStream)
  {
    line = file.readLine();
    output += "\"" + line + "\", ";
  }

  file.close();

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

  Response.contentType = "text/javascript";
  Response.write(output);
%>

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:

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

  $output = "var myArray = [";

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

  fclose($fp);

  $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.
0
 

Author Comment

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

Expert Comment

by:Zontar
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now