Force page scroll position to top at page refresh in HTML
For a simple plain JavaScript implementation: window.onbeforeunload = function () { window.scrollTo(0, 0); }
For a simple plain JavaScript implementation: window.onbeforeunload = function () { window.scrollTo(0, 0); }
If you’re using React Hook, UseEffect you can put the below changes in your component. It worked for me useEffect(() => { window.addEventListener(“beforeunload”, alertUser); return () => { window.removeEventListener(“beforeunload”, alertUser); }; }, []); const alertUser = (e) => { e.preventDefault(); e.returnValue = “”; };
window.onbeforeunload = function(event) { return confirm(“Confirm refresh”); };
You can use pageshow event to handle situation when browser navigates to your page through history traversal: window.addEventListener( “pageshow”, function ( event ) { var historyTraversal = event.persisted || ( typeof window.performance != “undefined” && window.performance.navigation.type === 2 ); if ( historyTraversal ) { // Handle page restore. window.location.reload(); } }); Note that HTTP cache … Read more
Try to use: location.reload(true); When this method receives a true value as argument, it will cause the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache. More info: The location object
window.location.reload(); in JavaScript <meta http-equiv=”refresh” content=”1″> in HTML (where 1 = 1 second).
New standard 2018-now (PerformanceNavigationTiming) window.performance.navigation property is deprecated in the Navigation Timing Level 2 specification. Please use the PerformanceNavigationTiming interface instead. PerformanceNavigationTiming.type This is an experimental technology. Check the Browser compatibility table carefully before using this in production. Check if page gets reloaded or refreshed in JavaScript const pageAccessedByReload = ( (window.performance.navigation && window.performance.navigation.type === … Read more