Different Counter Value for Each Page on Website

136 Views Asked by At

In code (link below), I would like to change some data so that the counter values for each page would be different.   At the moment, if this code is placed in another page of the site, the counter will show a number that continues the number of the previous page.

I have a website with /news-details?id=XXX example post ID structure. And I want each page to have its own specific counter, starting from 0.

Please, help.

My code is here (JS Fiddle): jsfiddle.net/zt73db5q

1

There are 1 best solutions below

2
Ankur Bajaj On

try something like this

var n = localStorage.getItem('on_load_counter');

if(n === null){
   n = {"pageid":0}
}

if (n["pageid"] === null) {
   n["pageid"] = 0;
}

n["pageid"]++;

localStorage.setItem("on_load_counter", n);

document.getElementById('counter').innerHTML = n["pageid"];