How to check that the current URL is in a specific domain in Javascript

To check if the current URL is in a specific domain in JavaScript, you can use the location.hostname property and the String.endsWith() method. For example, if you want to check if the current URL is in the example.com domain, you can use the following code:

if (location.hostname.endsWith('example.com')) {
  // The current URL is in the example.com domain
}

This code checks if the hostname property of the location object ends with 'example.com', and if so, it executes the code inside the if block. The hostname property contains the hostname part of the current URL (e.g. www.example.com), and the endsWith() method is used to check if the hostname ends with the specified string (in this case, 'example.com').

You can also use the String.includes() method to check if the hostname includes the specified string, rather than just ends with it. For example, the following code checks if the hostname includes 'example', regardless of the top-level domain:

if (location.hostname.includes('example')) {
  // The current URL is in the example.com (or any other example.*) domain
}