Getting a Script Error Every Time I Click a Link

Posted by Flip4Life on Stack Overflow See other posts from Stack Overflow or by Flip4Life
Published on 2012-09-19T03:23:21Z Indexed on 2012/09/19 3:37 UTC
Read the original article Hit count: 122

Filed under:

I have everything working perfectly on my site, but for some reason, I get an error message in console whenever I click a link anywhere on my site. The error has to do with this line of coding here:

jQuery(function($){
  $('.navbar a, .scroll a, .smoothscroll a').bind('click',function(event){
    var $anchor = $(this);

    $('html, body').stop().animate({
        scrollTop: $($anchor.attr('href')).offset().top
    }, 850,'easeInOutExpo');

    event.preventDefault();
  });
});

And the error I am getting is this:

"SCRIPT5007: Unable to get value of the property 'top': object is null or undefined custom.min.js, line 6 character 197"

The exact code it is highlighting is this part of the above code:

$('html, body').stop().animate({
    scrollTop: $($anchor.attr('href')).offset().top
}, 850,'easeInOutExpo')

All I know is that when I remove the above code, my scroll-to links stop working on pages such as these:

http://www.northtownsremodeling.com/things-to-know.php

You can see the popup error happen and stay in the console easily by going to a page with a filter like this:

http://www.northtownsremodeling.com/bathroom/

And clicking one of the filter buttons.

Ultimately, I am trying to make it so my scroll-to setting still works, but not have that error come up anymore. I made this script a long time ago, and I'm really confused as to what could be causing this error when everything is functioning perfectly otherwise?

Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript