Javascript Hit Counter

Monitor showing HTML Hit Counter Codes
HTML Hit Counter Codes

A Javascript hit counter is a very useful tool for keeping easy track of website traffic. While visitor logs will always give more in-depth information on the actual visitors to a site, using a Javascript hit counter to actually display how popular a site is can be a simple and dynamic part of the design of a web page.

Elements of a Javascript Hit Counter

A hit counter written in Javascript has several characteristics and elements similar to other "counting" elements such as an HTML countdown timer, including the following.

  • Font
  • Color
  • Size
  • Advertising or other messages

There are other decisions that have to be made about it, though. First, a hit counter can count each page view or it can count unique visitors to the site. The former is a good way to measure the popularity of a page (based on how often people come back to it) but the latter is a better measure of the reach of a site to a larger audience.

Second, like Cascading Style Sheets, the hit counter code may be put into the head section of the document or simply referenced to a ".js" document hosted on your site. If only one page is going to have the counter on it (such as the index page of a site) it may be easiest to simply include the code within the page. On the other hand, if there are several pages that will reference the hit counter, having each of them reference the .js file using code such as

<script type="text/javascript" language="JavaScript" src="PATH/TO/JS/Counter.js">

can save page loading time especially for larger sites.

How to Find a Hosted Hit Counter Written in Javascript

If you don't have the inclination or skills to create your own hit counter, there are many sites on the web ready to assist with the task. Usually they will begin by providing you with a series of drop-down menus containing the various options, such as:

  • Display type (pageloads or unique visitors)
  • Minimum number of digits (this can be important on high-traffic sites)
  • Counter digit color
  • Counter background color
  • Transparency
  • Counter digit font

Once these choices have been made, the site will usually cheerfully tell you that you are "only a step away from getting your free Javascript hit counter!" Then they require you to register with their site.

This is not necessarily a bad thing; it's entirely possible that this is a safe website. However, it is worthwhile to take the time to find out what they are doing with the information you give when you register. It may be a simple newsletter containing valuable information; it may be a series of annoying but benign spam emails that get sent to the contact web address.

In a worst-case scenario, however, the Javascript code is a doorway into your web server that can serve as a hacker attack which will then launch "malware" or other harmful software from your server. Checking the source of the free Javascript code for the web counter is worthwhile.

Roll Your Own

Even if you are not an experienced Javascript programmer, it is possible to find basic code for a hit counter that can be reliably put into your web page. Sites such as this one give the basic code behind the counter, as well as the few lines of script to put into the actual body of the page in order to display the counter on the page.

Without some tweaking of the code (which requires basic programming knowledge) the hit counter will be very basic. It will also usually rely on the use of "cookies", small files that are registered by web browsers to show they've been to the site before. Because cookies have been used to plant viruses and other malware on computers, many people turn off or purge cookies from their browsers on a regular basis. This can reduce the accuracy of the counter.

In all cases, an accurate reading of the visitor demographics to a site is only available through the logs. A hit counter, whether Javascript or some other technology, is a simple and effective way to liven up a web page with useful information.

More Information

For more information about website resources and tools like counters, check out the following LoveToKnow articles.

Javascript Hit Counter