0

I have this script adds an active class to a navbar link based upon the current page. The issue that I am having is it throws off an error when I access a page not in the navigation bar. The error is '$' is undefined. So I am assuming since it can't find the page name in the navbar, it is throwing an error.

I am using a .net master page. I have this script along with the navbar in the master page.

$(document).ready(function() {
    var current = location.pathname;
    $('.nav li a').each(function(){
        var $this = $(this);
        if ($this.attr('href').indexOf(current) !== -1) {
            $this.addClass('active');
        } else {
            $(this).removeClass( "active" );
        }
    })
});
Dan Nick
  • 514
  • 1
  • 9
  • 30

1 Answers1

0

Check if you have jquery installed properly on the page. If not include this before any of your scripts.

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
CodeThing
  • 2,580
  • 2
  • 12
  • 25