How to ensure you are in the homepage in vanilla Javascript?

372 Views Asked by At

There's a Greasemonkey script I run matching:

// @include     *://example.com/*
// @include     *://www.example.com/*

Yet some codepiece in this script 's relevant only to the site's homepage.

I tried to execute it only in the home page the following ways:

1)

if ( document.location === 'https://example.com' || document.location === 'https://www.example.com' ) {
    do stuff...
}

2)

if (window.location.href == window.location.origin) {
    Do stuff...
}

3)

if ( document.location == '/' ) {
    do stuff...
}

Yet these didn't help.


I thought of modifying the following code somehow:

if (window.location.href.indexOf('https://example.com' || window.location.href.indexOf('https://www.example.com') != -1) { // Do stuff...}

Maybe a modification with regex could ensure that anything else beyond the origin wouldn't count so the check will be: "if the document.location is only example.com, do stuff...".

Do you know a way?

0

There are 0 best solutions below