Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to know browser refresh or F5 has been hit

I am looking for a way to identify if user refreshes a page or hits F5 using Javascript (more specifically prototype Javascript library)...

I am coding an application that needs to record the number of refresh(es) or F5 on a web page for statistics purposes. A refresh or F5 in this case means the user is skipping or escaping doing his work on the page :) So the statistics helps them find how many of them are doing it :).. Kind of policing...

like image 1000
Abhishek Avatar asked Jan 25 '26 07:01

Abhishek


1 Answers

Put the following piece of code on each page (only then you can differ between refreshing/navigating):

var last = localStorage["lastPage"] || "",
    current = location.href;

if(last === current) { // last page is the same as this page
    // this is a refresh
]

localStorage["lastPage"] = current;

It only differs between navigating on the same domain and not doing so (i.e. going to another website and then going back to the same page counts as a refresh).

like image 136
pimvdb Avatar answered Jan 27 '26 20:01

pimvdb