Let’s say you want to have a link that sends the user back one page, but also refreshes the page so that last page’s content isn’t stale.
Here’s a way you can do it:
// On button press, or something else:
document.referrer ? window.location = document.referrer : history.back()
This looks to see if we have a
referrer, which is the last page that the user came from before this page. If there is, we navigate to that page.
If there isn’t, we fall back to using
history.back() as an alternative, which sends the user back without refreshing.
Subscribe to my newsletter!
A weekly round-up of new blog posts and updates to projects I’m working on.