1

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 Answers1

2

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"];
Ankur Bajaj
  • 143
  • 2
  • 11
  • Thx for your answer.But how can I try your modified code for different html pages? – Shekilander Sep 01 '19 at 10:43
  • Actually, just use `localStorage.getItem( ‘on_load_counter’ + window.location.href )` and `localStorage.setitem( ‘on_load_counter’ + window.location.href, n )`, as localStorage contains the same data _on your entiire domain_. – somethinghere Sep 01 '19 at 12:59