J82 J82 - 3 months ago 12
jQuery Question

Add class to <body> if url has /shop/

I'm working on a SquareSpace shop and unfortunately for me, no class gets added to the body for all shop pages. By this I mean any page that starts with

http://example.com/shop/


I would like to add a class to the body for all pages that start with this url whether it be
http://example.com/shop/item1
,
http://example.com/shop/category/category-name
, etc.

I tried this, but no class gets added to the
<body>
.

$(document).ready(function() {
if(url.indexOf('/shop/') > -1){
$("body").addClass("shop");
}
});


How can I make this work?

ABE ABE
Answer
$(document).ready(function() {
    if(window.location.href.match('example.com/shop')){
        $("body").addClass("shop");
    }
});
Comments