Skip to content

Instantly share code, notes, and snippets.

@lbramos
Created February 1, 2015 17:32
Show Gist options
  • Save lbramos/b1fd6f67915b3d6b942b to your computer and use it in GitHub Desktop.
Save lbramos/b1fd6f67915b3d6b942b to your computer and use it in GitHub Desktop.
Browser Back Button Behavior
// I needed a way to hijack the browser back button on a Ruby on Rails app,
// so users don't make a mistake thinking that they're editing a record instead of creating a new one
// Thanks to thecssninja.com site :)
(function(window, location) {
history.replaceState(null, document.title, location.pathname+"#!/nobackbutton");
history.pushState(null, document.title, location.pathname);
window.addEventListener("popstate", function() {
alert('To go back or edit the record, please use the buttons below!');
if(location.hash === "#!/nobackbutton") {
history.replaceState(null, document.title, location.pathname);
setTimeout(function(){
location.replace();
},0);
}
}, false);
}(window, location));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment