Add class to body based on the URL – jQuery

Get FREE Marketing Guides & Templates
// find the pathname
var urlPath = window.location.pathname;
//convert to lowercase
// remove the first character
urlPath = urlPath.replace(/\//g, '-').slice(1);
//remove file extension
urlPath = urlPath.replace(/\.[^/.]+$/, "");
// add class to body
// If body has no class its propably the homepage


Written by: Jake Lett
I share digital marketing tips and HubSpot tutorials to help marketers and business owners grow their business.


Related posts


Want to Get Email Updates of New Articles?

Join My Email Newsletter