I am using jQuery’s scrollTop() method to get the vertical position of the scroll bar on pageload. I need to get this value after the anchor in the url is executed (ex url: http://www.domainname.com#foo). I can use the following code and it works in Firefox and IE:
ex url: http://www.domainname.com#foo
$(document).ready(function() {
if ($(this).scrollTop() > 0) {
// then a conditional statement based on the scrollTop() value
if ($(this).scrollTop() > $("#sidenav").height()) { ...
But in Safari and Chrome document.ready() seems to execute before the anchor so scrollTop() returns 0 on page load in this scenario. How can I access the scrollTop() value on page load after the anchor executes in Chrome and Safari?
You might just want to do something quick-and-dirty like
setTimeoutfor however many milliseconds it takes to get it from Chrome or Safari reliablyNote that you can also use
if ($.browser.webkit), although this feature is deprecated and may be moved to a plugin instead of the main jQuery (in favor of feature detection).