Skip to content

Instantly share code, notes, and snippets.

@ajskelton
Created June 26, 2017 16:34
Show Gist options
  • Save ajskelton/0d06f065314eab0758a0531dd8ecd178 to your computer and use it in GitHub Desktop.
Save ajskelton/0d06f065314eab0758a0531dd8ecd178 to your computer and use it in GitHub Desktop.
Remove the hash from a url and keep the browser in the same scroll position
function removeHash () {
var scrollV, scrollH, loc = window.location;
if ("pushState" in history)
history.pushState("", document.title, loc.pathname + loc.search);
else {
// Prevent scrolling by storing the page's current scroll offset
scrollV = document.body.scrollTop;
scrollH = document.body.scrollLeft;
loc.hash = "";
// Restore the scroll offset, should be flicker free
document.body.scrollTop = scrollV;
document.body.scrollLeft = scrollH;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment