-2

I want to count the number of refresh of a webpage through jquery or javascript. I made research on it but did not get the perfect answer.

Can anyone help me, Please?

user3446322
  • 129
  • 1
  • 2
  • 11

3 Answers3

3
var _hash = window.location.hash;
if( _hash ){
var x = parseInt( window.location.hash.replace("#", ""));
x = x+1;
window.location.hash=x;
}else{
  window.location.hash = "1"
}
kag
  • 144
  • 1
  • 13
1

You can count with the help of local storage:

$(document).ready(function(){

   localStorage.refresh=localStorage.refresh!=null ? localStorage.refresh : 0;
   localStorage.setItem("refresh", parseInt(localStorage.refresh)+1);
   alert(localStorage.refresh);
});
Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
-1

You can't, because you can't write to files with pure JS. However, using $('body').bind('beforeunload',function(){}) you can detect a reload, wich you could save in a cookie. But you can't count the total number of reloads on you page from all users. Also the cookie approach obviously won't work for browsers with cookies disabled.

GeF
  • 70
  • 6