I don't want to detect if the user has pressed the back button on my page.
I want to detect if the user has pressed the back button on someone else's page and has gotten back to my page.
I need to reload the page if that's the case.
I don't want to detect if the user has pressed the back button on my page.
I want to detect if the user has pressed the back button on someone else's page and has gotten back to my page.
I need to reload the page if that's the case.
You can play with window.history
and maybe store some values for the user locally in localstorage
or a cookie. (For reference, MDN article)
For example, check the value of window.history.length
when the user is on your page; then navigate somewhere else; then hit "back" to return to your prior page. Now compare window.history.length
again -- it is different (at least for me, in Chrome).
TL;DR - this won' t be a foolproof method, but it might be a good enough hack for you.
Here's what I ended up doing. It works, but let me know if you have a better solution:
window.addEventListener('unload', unload)
function unload() {
localStorage.setItem('reload',1)
}
var reload = localStorage.getItem('reload')
reload = parseInt(reload,10)
if (reload) {
localStorage.setItem('reload',0) // false doesn't work here.
window.removeEventListener('unload', unload)
window.location.reload()
}