Liz Banach Liz Banach - 3 months ago 12
Javascript Question

How to add body class to front page & an internal page using jQuery?

The jQuery function below works properly, and adds a

full
class to the
<body>
HTML element on the
/signin/
page based on the URL.

// add full class to sign in page body based on url

$(function() {
var loc = window.location.href; // returns the full URL
if(/signin/.test(loc)) {
$('body').addClass('full');
}
});


I want to accomplish the exact same thing on the front/home page, or
/
URL within this same function. How do I do this?

Answer

Regexes are slow. A faster way:

function() {
    var loc = window.location.pathname; // returns the pathname
    if(loc == '' || loc == '/') {
        $('body').addClass('full');
    }
});