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
How to Restrict Past Dates in Html5 Input Type Date
How to Load All the Images from One of My Folder into My Web Page, Using Jquery/Javascript
Fetch() Unexpected End of Input
Check If Json Response Data Is Empty
React Jsx - Make Substring in Bold
Multiple Key Names, Same Pair Value
Passing Variable from One Js to Another Js File in Nodejs
Passing Multiple Parameters from Angular to Rest Service
Is There a Function in Lodash to Replace Matched Item
Detect If User Is Using Webview for Android/Ios or a Regular Browser
Loading Screen Gets Stuck While Processing Http Request
Regular Expression for Not Allowing Spaces in the Input Field
Remove Insignificant Trailing Zeros from a Number
Keep Selected Dropdown Result After Refreshing Page, Jquery
How to Pass Model Data in JavaScript
Changing the Browser Zoom Level