Check If Page Gets Reloaded or Refreshed in JavaScript

how detect refresh event through JavaScript on Chrome

My solution for this was to set a session token.

    var item = sessionStorage.getItem('pageHasBeenLoaded');
if (item) {
console.log('this is a reload');
}
sessionStorage.setItem('pageHasBeenLoaded', 'true');

Is there a way to detect a page reload successfully in javascript

I agree with Aaron McGuire's comment--rather than trying to detect a page load, then clear session storage, it'd be easier to clear session storage on page load. You could insert something quick like this at the top of your code:

window.onload = function() {
sessionStorage.clear()
}

How to get check if a page gets reloaded with js

You must remember to always pay attention when reading a new API docs, type in PerformanceNavigationTiming is a string and not a number so the correct way would be:

if (window.PerformanceNavigationTiming) {
console.info("window.performance works fine on this browser");

if (PerformanceNavigationTiming.type === "reload") {
console.info("This page is reloaded");
} else {
console.info("This page is not reloaded");
}
}

Detect difference between refresh and reload

use window.onbeforeunload function to check if refresh/reload is pressed and handled the event localStorage.clear()

window.onbeforeunload = function(){
var message = 'Are you sure you want to reload?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}


Related Topics



Leave a reply



Submit