user1717480
user1717480

Reputation:

jQuery if URL contains pathname

I have this script:

<script type="text/javascript">
$(document).ready(function() {

var url = location.pathname;

  if ("url:contains('message')") {
    $("a#none").attr("class","active");
  }

});        
        </script>

It nicely adds the class active to the url. However, it add the class active to the url even if the url does not contain the path message. What am I doing wrong?

Upvotes: 2

Views: 7154

Answers (1)

Ram
Ram

Reputation: 144669

That's because strings are truthy and :contains is a jQuery selector that doesn't do anything in your code, you can use indexOf method of the String object:

if (url.indexOf('message') > -1) {
    // $("#none").addClass("active");
}

Upvotes: 5

Related Questions