INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Counter Code

How do I write counter code that works from a local or network text file? by BillyRayPreachersSon
Posted: 12 Jul 04 (Edited 3 May 06)

This code will enable users (IE only - sorry!) to have a local counter that runs on their PC. It requires the FileSystemObject ActiceX control be allowed to run.

CODE

<html>
<head>
    <script type="text/javascript">
    <!--
        var fsObj = null;                        // holds a pointer to the FileSystemObject
        var counter = 0;                        // holds the counter value
        var counterFile = 'C:\\jsCounter.txt';    // holds the path and filename to the counter text file

        // returns 0 if no counter or NaN is found, the counter value otherwise
        function readCounter() {
            try {
                if (!fsObj.FileExists(counterFile))    return(0);
                var tempValue = fsObj.OpenTextFile(counterFile, 1).ReadLine();
                if (isNaN(tempValue) || tempValue == ' || tempValue == null) return(0);
                return(tempValue);
            }
            catch (err) {
                return(0);
            }
        }

        // returns false if write failed, true otherwise
        function writeCounter(counterValue) {
            try {
                //alert(counterValue);
                var counterFileObj = fsObj.OpenTextFile(counterFile, 2, true);
                counterFileObj.WriteLine(counterValue);
                counterFileObj.Close();
                return(true);
            }
            catch (err) {
                return(false);
            }

        }

        function init() {
            try {
                this.fsObj = new ActiveXObject('Scripting.FileSystemObject');
            }
            catch (err) { }

            if (!this.fsObj) {
                alert('You must allow the ActiveX object to load for the counter to work');
            } else {
                counter = readCounter();
                alert('Current counter: ' + counter);
                counter++;
                if (!writeCounter(counter)) alert('There was an error writing the counter');
            }
        }
    //-->
    </script>
</head>

<body onload="init();">
</body>
</html>

The file in the demo is a local file, but the path can be anything which resolves, like a network share, etc.

Enjoy!

Dan


Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch
http://www.codecouch.com/dan/

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close