Skip to content

Instantly share code, notes, and snippets.

@thanhtuong
Forked from w33tmaricich/back_button_override.js
Created December 16, 2016 07:45
Show Gist options
  • Save thanhtuong/be243a0b97a9c8e3bcbfaf0f50ddb0de to your computer and use it in GitHub Desktop.
Save thanhtuong/be243a0b97a9c8e3bcbfaf0f50ddb0de to your computer and use it in GitHub Desktop.
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function () {
history.pushState('newjibberish', null, null);
// Handle the back (or forward) buttons here
// Will NOT handle refresh, use onbeforeunload for this.
};
}
else {
var ignoreHashChange = true;
window.onhashchange = function () {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
// Detect and redirect change here
// Works in older FF and IE9
// * it does mess with your hash symbol (anchor?) pound sign
// delimiter on the end of the URL
}
else {
ignoreHashChange = false;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment