Link to home
Start Free TrialLog in
Avatar of cbutton
cbuttonFlag for United States of America

asked on

Read, write and modify a text file

Is there a way, using JavaScript, to create a text file on my server that I can read, write and modify?
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Are you talking about client side JavaScript or server side JavaScript?

It is possible with the latter but not the former.

Fritz the Blank
Avatar of cbutton

ASKER

Help me out, Fritz the Blank. I don't know anything about server-side JavaScript. What I want to do is count every time a certain function is executed and what its single argumnt was.
If you can run server-side scripting such as ASP, it is possible to write to a file on the server. However, if you are just running client-side JavaScript, there is no way that the client-side code can talk to the server until you submit the page.

Can you run server-side code for your site?

Fritz the Blank
Avatar of msa2003
msa2003

fritz_the_blank: what do you mean under server-side JS? WSH? Don't you think that WSH scripts are never used to make server-side applications?

cbutton: you must understand what JS can do and what can not. I'd advice you to read some JS book prior to make JS scripts.

Now I'll explain a little.

JavaScript is a scripting edition of Java language. They are many cases of JS use. JS is often used as a OS command language (like WSH) or even to write automation scripts in such applications like Corel DRAW! 9 and Adobe Photoshop 7.0. Another usage of JavaScript is writing scripts built in HTML pages to performe some interactive tasks (so-called Dynamic HTML or DHTML). Java scripting is closely integrated with Document Object Model (DOM) - the object model of the browser. I. e. java scripts (so-called "client-side", because they are run on the client side) could change some document or browser window properties, such as size, position, scrolling or document elements, such as input elements (buttons, text aread, etc.), <div> layers and other objects properties. For example, using JavaScript you could easily chech the user form contents, submit form or move the picture. But client-side JavaScript, rather than Java applets, could never directly ineract with file system or network. I. e. JS could never create or delete the file or open the network connection. The only way of use JS is to make interactive HTML contents.

Ofcourse if your provider's server's OS supports Java scripting you could place server-side JS CGI (Common Gateway Interface) script (such as Windows Scripted Host, WSH), but usually OS JS scripting is very limited and most providers even doesn't realize WSH support for CGI. Usually, Perl and PHP language are used for server-side CGI scripting.

This way, the answer sounds like "No", but if you will explain in details what you are about to do I think I (or fritz_the_blank ;)) could help you.
Not to get hung up on this, but the big distinction that needs to be made is that this needs to happen server side not client side, i.e., the client machine cannot write to a document on the server--it can only post data to the server and then have server-side code do the writing. I want to make certain that cbutton understands this. Once that is clear, we could then move on to the different flavours of JS.

Crawling before walking so to speak...

Fritz the Blank
msa2003 - [JavaScript is a scripting edition of Java language.] ?? No it's not.
http://www.dannyg.com/ref/javavsjavascript.html

IIS ASP nativly uses 2 languages VBscript and Jscript (or the Microsoft version of javascript).
Avatar of cbutton

ASKER

msa2003, I'm not totally new to JavaScript and I have read books on it. In fact I have written a lot of JavaScript code (which, by the way, is NOT a scripting edition of Java language). What I am NOT familiar with is server-side JavaScript. I guess my books are quite a bit out of date.

fritz_the_blank, yes, I do understand that I cannot write to a file with the JavaScript that I know. As I said above, hat I want to do is count every time a certain function is executed and what its single argumnt was.

Can you point me in the right direction? I understand programming (I was a programmer for about thirty years), and I can learn a new language relatively easily.
Okay, let's see if this would suit your needs.

Let's say that you have a function that is client side code and that you want to track the number of times it was executed and what the parameters were.

The first step would be to set up a client side variablesthat would hold a delimited string that adds the passed parameter each time the function is executed. You would then store that variable to a hiddend field.

Next, the form would have to be submitted to the server where the server-side scripting could grab the value of the hidden field, parse it out, and then write it to your server-side text document.

Of course, if the user fired your function numerous times but did not submit the page, then that data would be lost.

Is this the sort of thing that you are looking for?

Fritz the Blank
Avatar of cbutton

ASKER

Exactly, Fritz the Blank.
Avatar of cbutton

ASKER

Exactly, Fritz the Blank, except that I cnnot depend on the user to submit the form.
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If the user does not submit the form, or if you don't use client side scripting to submit the form, then there is now way for the data to be posted for the server to process.

Fritz the Blank
Avatar of cbutton

ASKER

Fritz the Blank, apparently I can't run asp's from my server. I uploaded it to my server. then I tried to view the page. I just got a blank page. No "Hello World!" message. Then I moved it to my "cgi" folder on the server, and then it said the page could not be displayed.

As for the user submitting a form. he isn't at all interested in my counts. He only wants to see the information about a certain dog or cat, which the function provides for him. I want to see how many people look at each animal.
No ASP then no joy unless you can run perl (with which I cannot help you).

Isn't there some sort of stats package on your server that tallies which pages are visited and etc?

Fritz the Blank
The first. As I understand, Fritz the Blank, your suggestion is to make a JavaScript submit a form. This has a little bottlneck because the user must receive script's responce and view this. Allthrough they are many other ways to do it. You may change 1x1 pixel image source or hidden iframe source to point to the server script with parameters delimited by '?' char. For example:

<html>
<head>
<script>
function someFunction(param)
{
  // .....
  document.images['img1'].src='../cgi-bin/counter.pl?param='+param+'&r='+parseFloat(Math.random());
}
</script>
</head>
<body>
<img name="img1" src="images/1x1.gif"></img>
<button onClick="someFunction('Hello, World!');">OK</button>
</body>
</html>

counter.pl is a Perl script which counts function calls (or I don't know what it is exactly to do) called with two parameters:

param - function parameter (assumed that it is string parameter, if it is a number, use parseInt() or parseFloat() functions to make a string);

r - random number. Used to avoid caching (may be replaced with "pragma: no-cache" statement in the server script responce).

The script performs designed procedures, then it generates blank 1x1 pixel (or not blank ;)) GIF as the response (it may simply read GIF from the file and post it's contents).

I could not provide a Perl script example, because I don't know what exactly it is about to do ;)

Test Perl script (like the one posted by Fritz the Blank) may look like:

#!/usr/bin/perl

use CGI qw(param);

print <<END_of_Start;
Content-type: text/html

<html><body><h1>Hello World!</h1></body></html>
END_of_Start

But I'm sure that it would not run from your server. Just bacause administrator needs first to assign "execute" permissions on scripts folder. Usually, they are two roots in scripts-enabled sited:

\wwwroot - root for static documents such as HTML's or images;

\scripts - root for scripts (alias "cgi-bin")

If you have these two folder in your FTP root, I'm sure that you could place scripts there ;-). But I think anyway you must first contact your hosting provider and ask it for scripts support. Usually the price of "scripting" hosting significantly differs from "non-scripting".

Good luck!

Serge
Avatar of cbutton

ASKER

First, I din't realize it but my hosting service provides me with Urchin reports, and one of those contains the information I need (along with other stuff, but that's okay because I can select out what i want).

I'm awarding Fritz the Blank with the points, because I learned a lot from his comments. Thanks also  to everyone else who commented.
Glad to have helped,

Fritz the Blank