Created
April 24, 2013 12:12
-
-
Save schjetne/5451674 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use: ><a href="list.html" class="js-back">Go back</a> | |
$('.js-back').on('click', function(evt) { | |
if (document.referrer != "") { | |
evt.preventDefault(); | |
history.back(); | |
} | |
}); |
For your second @todo : Only run history.back() if referrer is on same domain
$('.js-back').on('click', function(evt) { if (document.referrer.indexOf(window.location.host) !== -1){ evt.preventDefault(); history.back(); } });
@Pistil-Studio - Thank you! Works perfect :)
//testing
console.log('test')
first solution doesn't work, @Pistil-Studio may be ok
Thanks @Pistil-Studio. Works perfectly
To check if you can go back:
history.state.position > 0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Todo: Timeout to check if history.back() actually runs
Todo: Only run history.back() if referrer is on same domain