Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamedmoody/43c4c302050ec971408cddfdfe041f0c to your computer and use it in GitHub Desktop.
Save hamedmoody/43c4c302050ec971408cddfdfe041f0c to your computer and use it in GitHub Desktop.
# Inform the target page, with a url hash, that it can't go back
<form action="page2.html#no-back" method="post" />
<input type="text" name="data" value="The data" />
<input type="submit" value="Send data" />
</form>
//Javascript
// It works without the History API, but will clutter up the history
var history_api = typeof history.pushState !== 'undefined'
// The previous page asks that it not be returned to
if ( location.hash == '#no-back' ) {
// Push "#no-back" onto the history, making it the most recent "page"
if ( history_api ) history.pushState(null, '', '#stay')
else location.hash = '#stay'
// When the back button is pressed, it will harmlessly change the url
// hash from "#stay" to "#no-back", which triggers this function
window.onhashchange = function() {
// User tried to go back; warn user, rinse and repeat
if ( location.hash == '#no-back' ) {
alert("You shall not pass!")
if ( history_api ) history.pushState(null, '', '#stay')
else location.hash = '#stay'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment