Created
June 26, 2017 16:34
-
-
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
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
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