DEV Community

Zaw Htut Win
Zaw Htut Win

Posted on

Detect the home page in Wordpress with Javascript

There is a way to detect whether the current page is the home page using Javascript. Most of the answer on the internet advice to use PHP is_front_page method in functions.php . I felt it introduce unnecessary complication and messy code. Following is the javascript version. It detect the body tag containing the 'home' class.

document.addEventListener('DOMContentLoaded', function() {
    if (document.body.classList.contains('home')) {
        // This is the home page
        console.log('This is the home page');
    } else {
        // This is not the home page
        console.log('This is not the home page');
    }
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)