Javascript Code Hard Page Reload No Cache
Javascript Code to Properly Fully Reload a Web Page in Javascript so Cache is Bypassed and the Web Page Fully Reloads
Home
Short:
function reloadNoCache() { // remove existing t= and hash which may interfere with no cache reload let curUrl = window.location.href.replace(/([?&])t=\d+/, "").replace(/#.*$/, ""); // clean up any extra ampersands curUrl = curUrl.replace(/(&{2,})/, "&").replace(/\?&/, "?"); // generate the current timestamp let curTime = new Date().getTime(); // append the timestamp curUrl += (curUrl.indexOf("?") === -1 ? "?" : "&") + "t=" + curTime; // perform the hard reload window.location.replace(curUrl); }
A one line (very long line) version of this function.
window.location.replace(window.location.href.replace(/([?&])t=\d+/, "").replace(/#.*$/, "").replace(/(&{2,})/, "&").replace(/\?&/, "?") + (window.location.href.indexOf("?") === -1 ? "?" : "&") + "t=" + new Date().getTime());
source code home