Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Forked from schjetne/back-with-fallback.js
Created February 20, 2018 17:21
Show Gist options
  • Save mehrshaddarzi/acda366376ea0b559e27dae7845b21d3 to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/acda366376ea0b559e27dae7845b21d3 to your computer and use it in GitHub Desktop.
Code to navigate to previous page in history if it exists, otherwise go back to a default "back" location.
// Use: ><a href="list.html" class="js-back">Go back</a>
$('.js-back').on('click', function(evt) {
if (document.referrer != "") {
evt.preventDefault();
history.back();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment