Reload page on every visit, also when using the back-button
Do you want to reload your webpage one time, every time the page is visited, also when people are pressing the back-button? Simply use this code:
/*! Reloads page on every visit */
function Reload() {
try {
var headElement = document.getElementsByTagName("head")[0];
if (headElement && headElement.innerHTML)
headElement.innerHTML += "<meta http-equiv=\"refresh\" content=\"1\">";
}
catch (e) {}
}
The code above does not work in mobile safari. To force mobile safari to reload the page one time, simply add these lines of code:
/*! Reloads on every visit in mobile safari */
if ((/iphone|ipod|ipad.*os 5/gi).test(navigator.appVersion)) {
window.onpageshow = function(evt) {
if (evt.persisted) {
document.body.style.display = "none";
location.reload();
}
};
}Written by Axel Ferdinand Giæver
Related protips
4 Responses
OMG!! Super THANK YOU FOR THIS!!!!!!!!!!! you saved my life!!!
THANK YOU!!!!!!!!!!!!!
Great!
Thank you very much. No other 'solution' did work -cache headers and so on...
how make to reload only on back/next button click? I am almost crazy searching for a solution!
Thanks
try this
$(window).bind("pageshow", function(event) {
if (event.originalEvent.persisted) {
location.reload();
}
});
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside