HTML Visitor Counter Source Code

HTML Visitor Counter Source Code
Code Snippet:Website visitor counter
Author: MohammadTaseenKhan
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 1,610
License: MIT
Edit Code online: View on CodePen
Read More

This HTML source code helps you to create a simple website visitor counter. Basically, it counts the visits for a single user using the local storage API. It checks if there is a page_view entry in localStorage. If there is, the code increments the counter by 1 and updates the localStorage entry.

If you are working on a tracking website visit statistics project, this code snippet might be helpful for you.

How to Create a Simple HTML Visitor Counter

1. Begin by including the necessary CSS libraries in the <head> section of your HTML file. Add the provided CDN links for the Meyer Reset and Tailwind CSS to ensure proper styling.

<link rel="stylesheet" href="">
<link rel='stylesheet' href=''>

2. Create the HTML structure for your visitor counter. Place it where you want the counter to appear. You can customize the layout and positioning according to your project’s design.

<div>Website visit count:</div>
  <div class="website-counter"></div>
 <button class="button" id="reset">Reset</button>

3. Copy and paste the following CSS code into your project’s stylesheet. This will ensure the counter and reset button are styled appropriately.

/* Center child elements */
.website-counter {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

/* Styles for website counter container */
.website-counter {
  background-color: #ff4957;
  height: 50px;
  width: 80px;
  color: white;
  border-radius: 30px;
  font-weight: 700;
  font-size: 25px;
  margin-top: 10px;

/* Styles for reset button */
#reset {
  margin-top: 20px;
  background-color: #008cba;
  cursor: pointer;
  font-size: 18px;
  padding: 8px 20px;
  color: white;
  border: 0;

4. Finally, insert the JavaScript code at the end of your HTML file, just before the closing </body> tag. This script handles the counter functionality and the reset button.

var counterContainer = document.querySelector(".website-counter");
var resetButton = document.querySelector("#reset");
var visitCount = localStorage.getItem("page_view");

// Check if page_view entry is present
if (visitCount) {
  visitCount = Number(visitCount) + 1;
  localStorage.setItem("page_view", visitCount);
} else {
  visitCount = 1;
  localStorage.setItem("page_view", 1);
counterContainer.innerHTML = visitCount;

// Adding onClick event listener
resetButton.addEventListener("click", () => {
  visitCount = 1;
  localStorage.setItem("page_view", 1);
  counterContainer.innerHTML = visitCount;

That’s all! hopefully, you have successfully integrated this HTML Visitor Counter source code on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link