Ching Ching
Ching Ching

Reputation: 217

Jquery addClass based on relative URL

I have a problem about addClass to div based on relative url.

this is my code

<div id="top"></div>

if (window.location.href == "product.php?p=blablabla") {
    $("#top").addClass(".topscroll");   
}
else { }

.topscro {background:red; }

blablabla is relative url that can change based on PHP $_GET,

what's wrong with my code?

Upvotes: 0

Views: 39

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can't check for equality as it will have the complete url of the page instead check whether href contains the pattern

if (window.location.href.indexOf("product.php?p=blablabla") > -1) {
    $("#top").addClass(".topscroll");
}

Upvotes: 1

Related Questions