Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Created March 7, 2014 10:02
Show Gist options
  • Save obenjiro/9408826 to your computer and use it in GitHub Desktop.
Save obenjiro/9408826 to your computer and use it in GitHub Desktop.
bugfix for browsers with weird scrollTop restoration after refresh policy
// weird Chrome, Y.browser bug with position been saved
// browser jumps to hash id and then jumps to last scrolled position
$(window).on('load', function(){
setTimeout(function(){
var initialUrlHash = getInitialUrlHash();
if (initialUrlHash) {
document.body.scrollTop = $(initialUrlHash).offset().top;
}
},0);
});
// weird Chrome, Y.browser bug with position been saved
// browser jumps to hash id and then jumps to last scrolled position
$(window).on('unload', function(){
var initialUrlHash = getInitialUrlHash();
if (initialUrlHash) {
document.body.scrollTop = $(initialUrlHash).offset().top;
}
});
function getInitialUrlHash() {
var url = window.location.hash;
return (url.match(/#\w+/) || [])[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment