Liz Banach Liz Banach - 1 year ago 77
Javascript Question

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

The jQuery function below works properly, and adds a

class to the
HTML element on the
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)) {

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 Source

Regexes are slow. A faster way:

function() {
    var loc = window.location.pathname; // returns the pathname
    if(loc == '' || loc == '/') {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download