6

I'm receiving the error:

body.scrollTop is deprecated in strict mode. Please use 'documentElement.scrollTop' if in strict mode and 'body.scrollTop' only if in quirks mode.

My code is:

$(document).ready(function(){

    //Animates Scrolling to anchor
    function scrollToAnchor(aid){
        var divTag = $("div[name='"+ aid +"']");
        $('html,body').animate({scrollTop: divTag.offset().top},'slow');
    }

    //If Checking out as guest, scroll to Shipping Information
    $("#ReadDescription").click(function() {
        scrollToAnchor('longdescreadmore');
    });

});

How can I edit my code to use this documentElement.ScrollTop?

7
  • considered using document.scrollTop instead of scrollTop ?
    – njzk2
    Jan 21, 2014 at 20:31
  • 1
    Possible duplicate stackoverflow.com/questions/19635188/…
    – sgtdck
    Jan 21, 2014 at 20:31
  • You are looking for document.documentElement. That's the element created by the <html> tag. Jan 21, 2014 at 20:36
  • @DaggNabbit That confused me. Heh. Where do I put this at?
    – user2433689
    Jan 21, 2014 at 20:38
  • @user2433689 I guess you'd want $('html') instead of $('html,body') in strict mode. Jan 21, 2014 at 20:39

1 Answer 1

14

Dagg Nabbit gave the solution. Change

$('html,body').animate({scrollTop: divTag.offset().top},'slow');

to

$('html').animate({scrollTop: divTag.offset().top},'slow');

if you want to avoid the deprecation warning in Chrome. (Why is body.scrollTop deprecated?)

It works because documentElement is the html node:

$('html')[0] === document.documentElement //-> true
$('body')[0] === document.body            //-> true

But your code is working now (albeit with a warning) and it will keep working when Chrome removes the "quirky" behavior. You shouldn't change your code if you want to continue supporting browsers that use body.scrollTop to represent the scrolling viewport in standards mode (older Chrome and Safari, I think).

3
  • 1
    Sam is right. If you leave the body part out of the selector, Safari 7.0.1 (and probably others) won't scroll anywhere.
    – Vestride
    Feb 4, 2014 at 2:01
  • So the point here is - don't change the code to suppress the warning?
    – jono
    Feb 21, 2014 at 1:47
  • 1
    @Jon, code like this is already targeting both the standard and nonstandard behavior (html,body), so it doesn't need to be changed.
    – sam
    Feb 23, 2014 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.