Website Visitor Counter Code Javascript Codepen

Website Visitor Counter Code Javascript Codepen

5 min read Jun 18, 2024
Website Visitor Counter Code Javascript Codepen

Website Visitor Counter Code with JavaScript (CodePen)

This article will guide you through creating a simple yet effective website visitor counter using JavaScript. We'll utilize CodePen, a popular online code editor, for building and testing our code.

Setting up the HTML Structure

First, create a basic HTML structure within your CodePen project. This will act as the visual representation of the counter:

  Website Visitor Counter




This code sets up the basic layout:

  • A div with id="counter" to contain the visitor count.
  • An h1 tag displays the "Visitors" label.
  • A span with id="visitorCount" will hold the actual visitor count number.

Implementing JavaScript Logic

Now, let's write the JavaScript code to manage the visitor count:

const visitorCountElement = document.getElementById("visitorCount");
let visitorCount = 0;

// Load the visitor count from local storage (if available)
if (localStorage.getItem("visitorCount")) {
  visitorCount = parseInt(localStorage.getItem("visitorCount"), 10);

// Update the visitor count on the website
visitorCountElement.textContent = visitorCount;

// Increment the visitor count and store it in local storage
function updateVisitorCount() {
  visitorCountElement.textContent = visitorCount;
  localStorage.setItem("visitorCount", visitorCount);

// Call the update function on page load


  1. We select the span element where the count will be displayed using document.getElementById("visitorCount").
  2. We initialize a variable visitorCount to 0.
  3. We check if a visitor count is stored in local storage (for persistent counting). If it exists, we retrieve it and parse it as an integer.
  4. We update the visitorCountElement's text content with the retrieved or initialized visitorCount.
  5. The updateVisitorCount function increments the count, updates the displayed count, and stores it in local storage for future use.
  6. Finally, we call updateVisitorCount immediately on page load.

Testing on CodePen

Paste the HTML and JavaScript code into your CodePen project. You should see the visitor count displayed as "Visitors: 1". Refreshing the page will increment the count, and it will persist across sessions due to local storage.

Limitations and Considerations

  • Browser Security: Local storage is specific to the user's browser. This approach won't track visitors across different browsers or devices.
  • Accuracy: This code relies on the updateVisitorCount function being called each time a visitor loads the page. However, multiple page loads in quick succession may result in duplicate counting.
  • Server-Side Solution: For more accurate and robust visitor tracking, consider using a server-side solution (e.g., databases, analytics platforms) to store and manage visitor counts.


  • Visual Styling: Customize the counter's appearance with CSS to improve its aesthetics.
  • Reset Button: Add a button to allow resetting the counter to 0.
  • Real-time Updates: Explore real-time technologies like web sockets to update the counter dynamically for each visitor.

Remember, this is a basic example. For a more sophisticated visitor counter, consider employing server-side technologies and analytics tools.

Related Post