Search code examples
javascriptjqueryrefreshoffsetonready

jQuery: How can I detect offset that page will return on refresh?


If you use $("body").offset() onready the result is always 0, even if the url includes an anchor.

Is there a better way to get the offset of where the page will actually resolve to?

Thanks!


Solution

  • Thats because $("body").offset() returns the top and left values of the body relative to the page.

    You probably want to use

    $(window).scrollTop();
    

    if you want the value of the scroll position.

    //this will alert the scroll pos on load
    $(document).ready(function(){
    
        var scrollPos = $(window).scrollTop();
        alert(scrollPos);
    
    });