1

I want a smooth scroll to internal links. Works fine in firefox, but in Chrome I get an error "body.scrollTop is deprecated in strict mode"

My code was

$(function() {
   $('a[href*=#]:not([href=#])').click(function() {
      if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
       && location.hostname == this.hostname) {
         var target = $(this.hash);
         target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
         if (target.length) {
            $('html,body').animate({scrollTop: target.offset().top}, 1000);
            return false;
         }
      }
   });
});

The problem, however, is after I changed this

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

to

$('html').animate({scrollTop: target.offset().top}, 1000);

the links don't work at all, and there is no error / warning in the console.

Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
  • 2
    Possible duplicate: http://stackoverflow.com/questions/21268450/body-scrolltop-is-deprecated-in-strict-mode-please-use-documentelement-scrollt – Ryan Feb 19 '14 at 16:16
  • @RyanPilbeam The solution mentioned there is to change $('html,body') to $('html') which is precisely my problem - the old code works in Firefox & not in chrome, but after changing it, it doesn't work in either, with no error / warning – KrisPrajapati Feb 19 '14 at 16:19
  • 1
    I can't imagine why there would be a strict mode error about a DOM property. – cookie monster Feb 19 '14 at 16:21
  • ...and I don't get that error in Chrome. http://jsfiddle.net/qr9z7/1/ Which versions of jQuery and Chrome are you using? – cookie monster Feb 19 '14 at 16:25
  • @cookiemonster getting the same warning in that fiddle. jQuery 2.1.0, Chrome Version 32.0.1700.107 m. – KrisPrajapati Feb 19 '14 at 16:31
  • I get the same error in that fiddle, same version, but not here: http://jsfiddle.net/98pNu/1/ – Ryan Feb 19 '14 at 16:33
  • I'm using Chrome 31, so that must be the difference. – cookie monster Feb 19 '14 at 16:33
  • @RyanPilbeam: You removed `body` from the selector. – cookie monster Feb 19 '14 at 16:35
  • Not working, with warning: $('html,body') // this works in firefox Not working, no warning $('html') or even $(document.documentElement) – KrisPrajapati Feb 19 '14 at 16:37
  • As a temporary solution, you could wrap the call in a `try/catch`. – cookie monster Feb 19 '14 at 16:38
  • Chrome 32 at least said that body.scrollTop was depreciated and to use documentElement.scrollTop (which by the way doesn't do crap either). I updated to Chrome 33 today and now the error is gone, and the link just doesn't move. Great.. – Sparatan117 Feb 21 '14 at 03:37

1 Answers1

0

Keep 'html,body' to target more browsers.

As long as Chrome is showing the "deprecated" warning (not an error), it's still responding to body.scrollTop and should be scrolling like you expect.

I think the confusion is because Chrome 32 warns about the deprecated feature but Chrome 33 uses only the deprecated feature with no warning.

sam
  • 40,318
  • 2
  • 41
  • 37