Tom Lee Tom Lee - 2 months ago 5
CSS Question

Query string in url with :

$("#menu a").each(function(index) {
if($.trim(this.href) == window.location.href) {
$('div', this).addClass("cc");
}
});


This is work when go to

example.com/foo/foo/


When navigate to second page for example

example.com/foo/foo/p:2


This does not work. How to make that query.

Answer

use .indexOf function to compare URLs.

$("#menu a").each(function(index) {
    var currentURL = window.location.href;
    var aHref = $.trim(this.href);
    if(currentURL.indexOf(aHref) !== -1) {
        $('div', this).addClass("cc");
    }
});