Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hit Count via HTML Coding Only 1

Status
Not open for further replies.

dwong

Programmer
Aug 8, 2001
20
AU
Hi Everyone,

I have an unusual request. We run a Employee Satisfaction group in our company and we don't have any budget to create websites both on the Internet and our Intranet. So, we decided to create our own Web page (basically a HTML document) residing on the LAN common drive.

We want to have a hit count facility. Because the page is on a common drive, my understanding is:
(1) Unable to join Hit Count websites (ie. sites that keeps track of counts for you), as our HTML doc is not on the Internet.
(2) Unable to use ASP.

If anyone knows how I can implement a crude hit count using native HTML or Javascript code that would be great! I just need to keep the count on a file on the common drive.

Sample codes would be appreciated.

Thanks and regards...
 
Well, if it's crude you're after, this is for you:

Create a simple text file (in notepad) that has just a number ie 1 in it. Save the file as something like c:\counter.txt

Then create your html page and use Javascript and the windows filesystem object to read and write your counter file.

Code:
<html>
<head>
<script>
//This code reads the c:\counter (remember the double slashes though) file and extracts the first line (your number)
Code:
function GetCount(){
   var fso, f, r;
   var ForReading = 1, ForWriting = 2;
   fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
   f = fso.OpenTextFile(&quot;C:\\counter.txt&quot;, ForReading);
   r =  f.ReadLine();
   return(r);
}
//This code takes a number as an argument (currVal), adds 1 to it and writes the result to the file
Code:
function IncrementCount(currVal){
   var fso, f, r;
   var ForReading = 1, ForWriting = 2;
   fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
   f = fso.OpenTextFile(&quot;C:\\counter.txt&quot;, ForWriting);
   f.WriteLine(parseInt(currVal) + 1);
}
//This code is called on startup, reads the current value, writes the result out to a DIV tag on the page and increments the counter
Code:
function doGetCount(){
  var resultDiv = document.all.countDiv;
  resultDiv.innerText = 'Counter: ' + GetCount();
  IncrementCount(GetCount());
}
</script>
</head>
<body onLoad=&quot;doGetCount()&quot;>
<div id=&quot;countDiv&quot;>Counter: 0</div>
</body>
</html>

Now, the downside - when you run this code, everytime you - or anyone - runs this code they will be prompted with a message:
An ActiveX control on this page might be unsafe to interact with other parts of the page. Do you want to allow this interaction? [YES] [NO]

The script will only run if the user clicks [YES].

Worth the hassle? Up to you, hope this helps.
 
Fantastic... Thabks for your help!
 
Thank you all for these. ON closer look is there anyway to turn off the Active X message that 'dwarfthrower' mentioned above?
 
Not from the server, if that was possible than certain pages on the internet would be free to format your hardrive everytime you visited, or other less noticeable, but horribly pain-in-the-*** tricks.

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Why is that you can't use ASP ?
you can easily have the PWS or IIS for win2000 installed for free and then run your pages with some server side support.
The only problem is that no more than 10 users can be connected at the same time.
If you have a group of 30-50 people you should be fine if the page is not highly demanded.

Now your other best option is defintely get Apache/PHP support or Tomcat and JSP, all for free on the Internet.
Is just a matter of choice and support from your LAN team.
We couldn't install Apache server in our computers no matter what argument we used.The LAN team just didn't allow us to do it.


grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Try looking at this posting. I've just posted an answer there which might be of use to you.

thread216-564869

Humble Seeker
-----------------------------------------------------
The longest journey begins with the smallest step.
 
Sorry just re-read your original post, and the solution I just suggested wouldn't work. Sorry.

Have you tried looking to see if your server (if your using a server) has a usage tracker?


Humble Seeker
-----------------------------------------------------
The longest journey begins with the smallest step.
 
dwong,

don't know if this is late, but if you want to get rid of those pesky active x control messages, and this is an internal use only site, then make it an hta file. hta is basically an html file but works like an application. becuase it is an application you don't need the permissions to run active x.

hope that helps.
pc
 
Dwarfthower,

I tried to use your solution, but I am getting an error. It says that it cannot find the file (the counter.txt) and it is in the same folder as the page. Is there anything I need to change (other than removing the C:\ before counter.txt)? THanks.

pc
 
You'll need to use the fully qualified file path to your counter.txt file. If you use just &quot;counter.txt&quot;, the fso Activex object will look for it in whatever it's starting dircectory is (I have no idea, could be WINNT, could be TEMP, could be anything). So if you have counter.txt in &quot;d:\web_pages\my_test_site\resources&quot;, then you need to use &quot;d:\\web_pages\\my_test_site\\resources\\counter.txt&quot; as the argument. It's not like a URL where relative addressing will get you close enough.
 
Dwarfthower-

Is there any way around that? I am using this for a company intranet, and it is going to live on a shared drive. The biggest problem (and I don't work in IT so I can't fix it) is that not everyone's share drive is mapped to the same letter. Therefore, I can't make the path S:\\file\\counter.txt. In vbscript I have been able to get around this by writing another call to GetPath (which basically goes from the folder the HTML file is in an goes up a folder and then goes back into the folder:

for example:
function GetPath
dim fs
set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
GetPath = fs.GetAbsolutePathName(&quot;..\..&quot;)
set fs = nothing
end function

Is there anything similar in JavaScript? Thanks
 
You can set your VBScript to return the path. Then call the VBScript from JavaScript.
 
I've tried and it doesn't seem to work. I'm really new at all this, can you help with the syntax?

<script language=&quot;VBSCRIPT&quot;>

function GetPath
dim fs
set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
GetPath = fs.GetAbsolutePathName(&quot;..\..&quot;)
set fs = nothing
end function
</script>

<script language=&quot;JAVASCRIPT&quot;>

function GetCount()
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
//this is where the JavaScript should call the VBScript (I think)
f = fso_OpenTextFile(&quot;GetPath('counter.txt')&quot;, ForReading);
r = f.ReadLine();
return(r);
}

function IncrementCount(currVal)
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
//this is where the JavaScript should call the VBScript (I think)
f = fso_OpenTextFile(&quot;GetPath('counter.txt')&quot;, ForWriting);
f.WriteLine(parseInt(currVal) + 1);
}

function doGetCount()
{
var resultDiv = document.all.countDiv;
resultDiv.innerText = 'Counter: ' + GetCount();
IncrementCount(GetCount());
}

</script>

thnaks a bunch.
 
On second thought... that VBSCRIPT just sits out there like dog's tentacles. Why not rewrite it in JS for consistency's sake:
[tt]
<script language=&quot;JAVASCRIPT&quot;>

function GetPath(){
var fs = CreateObject(&quot;Scripting.FileSystemObject&quot;);
var retValGetPath = fs.GetAbsolutePathName(&quot;..\..&quot;);
fs.close();
return retValGetPath;
}
</script>
[/tt]
 
Dwarfthower-

Thanks, that does make more sense. I am getting an error when I run it though. THe error is &quot;Object Expected&quot; and it refers to the line:

var fs = CreateObject(&quot;Scripting.FileSystemObject&quot;);

Also, how do I call that funciton within another function (see my previous post with the function inside of the other, is that correct?)

Thanks for all your help.
 
Yep, sorry - my bad. Just goes to show I shouldn't go round translating VB to JS in my head without testing.

var fs = CreateObject(&quot;Scripting.FileSystemObject&quot;);

Sould be:

var fs = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);

And what you have for calling a function inside a function is fine - just like you would alert(Math.random());
 
Dwarfthower,

You have been a great help. I tried the Javascript option, but it still doesn't want to work. I was thinking, however, that if I used the computer name to get the path instead of the drive letter (remember this is an intranet) to locate the text file, that would work.

I tried this, but it didn't work. Is the syntax different if I am using the computer name instead of the drive letter?

Example:

function GetCount()
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
f = fso_OpenTextFile('\\COMPUTERNAME\\FILE1\\COMMON\Analytics\\Website\\counter.txt', ForReading);
r = f.ReadLine();
return(r);
}

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top