Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


simple hit counter in Javascript

Posted on 2006-04-05
Medium Priority
Last Modified: 2008-02-01
i want a simple hit counter of my site in Javascript
i want to count every hit(refresh,etc), NOT unique visitator
Question by:fradolcino
LVL 35

Expert Comment

ID: 16381343
You'd have to store the number of hits on the server...

Otheriwse each person would just get THEIR hit count...not the number of times other people had hit the site too

So it's probably better done with JSP/ASP/PHP/etc

LVL 14

Accepted Solution

sam2912 earned 240 total points
ID: 16423483
Basically, you can't have a counter without any server-side code.

If you want to have some fun, you can try this instead:
(save the page as index.html as the Refresh Link will redirect to index.html, or you can change the target page)

<p id="counter">&nbsp;</p>
<p><a href="#" onclick="refreshPage()">Refresh Page</a></p>
<script type="text/javascript"><!--
var num = parseInt(getQueryVariable("n"));
if(isNaN(num)) num = 0;
function refreshPage() {
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return unescape(pair[1]);
  return "";

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question