Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS difference between enter page and refresh page

Is it possible to detect situation when page is entered and when the same page is refreshed

if (entered) alert("hi");
if (refreshed) alert("you've refreshed");

Somehow there are some little differences between page rendering when entered and when refreshed and it would be much easier to detect the case than to debug it for me (if its even possible - maybe some browser optimization stuff is causing it).

like image 658
OPOPO Avatar asked May 20 '26 03:05

OPOPO


1 Answers

This isn't an ideal solution, but if your page can load in under 5 seconds than this will work, and assuming you are not navigation to another page, then returning within 5 seconds.

window.onbeforeunload = function(){
  window.sessionStorage.setItem('lastvisit', new Date().getTime());
}

var lastVisit = +window.sessionStorage.getItem('lastvisit');

var isRefresh = (new Date().getTime() - lastVisit) < 5000;
console.log(isRefresh);
like image 133
Justin Bicknell Avatar answered May 22 '26 17:05

Justin Bicknell