Created
March 7, 2014 10:02
-
-
Save obenjiro/9408826 to your computer and use it in GitHub Desktop.
bugfix for browsers with weird scrollTop restoration after refresh policy
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
// 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