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?

1 Answers1

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).

Community
  • 1
  • 1
sam
  • 40,318
  • 2
  • 41
  • 37
  • 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 04 '14 at 02:01
  • So the point here is - don't change the code to suppress the warning? – jono Feb 21 '14 at 01: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 '14 at 16:44