PDA

View Full Version : Javascript counter


Whiteknight
01-29-2004, 07:49 PM
I'm working on a javascript textcounter, for times when SSI isnt available for using a CGI script. here is my code so far, I have been working on it for a while, and can't seem to work the bugs out.

also, I'm not even sure if the I/O functions I am using even exist. I found a reference about them, but maybe that is my problem....


function counter()
{
//Path to data file.
var path_to_data = "/var/www/html/data/sitedata.txt";
var count; //the count variable
var count_file; //the file pointer to the data file
var file_status; //boolean file status indicator

//sets the pointer "count_file" to the indicated path
count_file = new File(path_to_data);

//determines that no previous lock is in place, and if not,
//opens the file for reading, or creates a new, blank file.
if( project.lock() );
{

//if the file does not exist, the file is created
file_status = count_file.open("r");
if(file_status = 0)
{
count = 0;
}
else
{

//if the file does exist, read the number, and save it to count
count_file.setPosition(0);
count = count_file.readln;
count_file.close();
}

//file is unlocked for more use
project.unlock();
}

//count is incremented
count += 1;

//project file is locked again
if( project.lock() )
{

//file is erased, and new count value is entered
count_file.open("w");
count_file.writeln(count);
count_file.close();
}

//file is unlocked
project.unlock();

//count value is returned to page.
return count;
}


Ideally, once I get this simple version working, I will begin work on a more versatile script that can count for multiple pages (either by having multiple data files, or by having a single datafile that is searched or something. but i can worry about that later.

anybody see my problem?

Whiteknight
01-31-2004, 05:28 PM
well, I'll just spare everybody the suspense, I dont think that this code will ever work at all, and am instead looking to ActiveX for my javascripting needs.

gish
01-31-2004, 06:41 PM
ActiveX may be a little extreme for a counter. WHy not use perl, or php if all you are doing is a counter.

Whiteknight
02-01-2004, 01:01 PM
because some servers I work on are not SSI or PHP enabled.

its a sad truth, but not all the hardware in the world can handle all the wild server-side processing that we need.

gish
02-01-2004, 11:57 PM
what do you mean by that?

Whiteknight
02-02-2004, 06:10 PM
for instance, there is one server that I am managing, and it is old as crap. I think it is an old 486 with just bearly enough ram to run an older version of apache (with little to no SSI).

It is used for some really simple tasks, so i'm not worried about it's poor stats, but it simply doesnt have the abilities to run most things. for instance, it doesnt have PHP, mySQL, Perl (although I could probably add it if i tried hard enough...)

anyway, my reasoning doesnt really matter so much as the goal: I'm going to make a text-based JavaScript hitcounter one way or another.

jemfinch
02-03-2004, 12:47 AM
No, you probably won't. The simple fact is that Javascript is run client-side, and there's no way on the client-side to know how many hits a server has gotten on a page. Sure, you can download a file that shows the number of hits a page has gotten and parse that, but that file still has to be updated somehow, and that's going to mean spawning a process (or using an SSI) on each request for that page.

What you're trying to do is simply impossible.

Jeremy

Whiteknight
02-03-2004, 02:32 AM
depending on where I go, or who i ask, i always get a different answer. several sites that I have been to have alluded to a server-side variety of JS that has file i/o abilities.

i dont mind spawning a process, if thats what it takes, but i really want to find a way to make simple text counter without SSI, PHP, mySQL, or Perl.

plus, i dont have a deadline so i will just keep researching till i find the answer.

gish
02-03-2004, 12:25 PM
in order to use server side JavaScript, JScript, whith is the server side version of javascript. You will need to run ASP scripts. The bottom line is, you will need some sort of server side scripting. Install PERL.....you can have a text counter with 2 lines of code.

Whiteknight
02-03-2004, 04:45 PM
i know i know. i like to make things harder then they already are, and i hate hearing that something is impossible.

meh, i'll work on it.

gish
02-04-2004, 01:19 AM
it is easy.....relax.....dont make it more difficult than it needs to be.

skidooer
05-31-2004, 05:28 PM
Originally posted by Whiteknight
I'm going to make a text-based JavaScript hitcounter one way or another.
Have fun with that.

Originally posted by Whiteknight
but i really want to find a way to make simple text counter without SSI, PHP, mySQL, or Perl.
Put this in your crontab then.

#!/bin/sh
FILENAME=index.html
HTTPROOT=/
FSROOT=/var/www
LOGFILE=/var/log/apache/access.log

for i in $FILENAME; do
COUNT=`grep -c "GET $HTTPROOT$i" $LOGFILE`
sed -e "s/<\!--COUNTER-->[0-9]\+<\!--\/COUNTER-->/<\!--COUNTER-->$COUNT<\!--\/COUNTER-->/g" < "$FSROOT/$FILENAME" > "$FSROOT/$FILENAME.TMP"
mv "$FSROOT/$FILENAME.TMP" "$FSROOT/$FILENAME"
done

You may have to modify it to suit your environment.