diacetyl
diacetyl

Reputation: 25

stabilize hit counter number (aka total visits) on tumblr

I tried to create a hit counter for my Tumblr.
However it's only changes the number on my main browser, if I switch to other browsers it would reset and not keeping the total number everytime I reload my blog.
Basically it would show that I have 42 visits (because I reloaded it 42 times on my main browser).
But when I open it on other browsers, it resetted to 1 (because it's a new/seperate browser).
Please give me some solution for this! Thank you!
HTML

<script defer src="hit-counter.js"></script>
    <link rel="stylesheet" href="hit-counter.css" />
    <span class="website-counter"></span>

CSS

.website-counter {
  color: black;
  font-weight: bold;
  font-size: 12px;
  font-family: verdana;
}

JavaScript

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

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

Upvotes: 0

Views: 59

Answers (0)

Related Questions