-1

Hi i need to do if Statement for pathname. That's how i trying to do this but it's doesn't work.

if (pathname == "/") {
    category = 'home';
    pagetype = 'homepage';
}

If pathname is like that the script should show category : home and pagetype : homepage. Thanks for help

Priya
  • 1,359
  • 6
  • 21
  • 41
Tori
  • 41
  • 1
  • 2
  • 7

1 Answers1

0

To get the URL you need to use window.location

For the URL "http://www.google.com/images":

window.location.href; // http://www.google.com/images
window.location.hostname; // www.google.com
window.location.pathname; // /images
window.location.protocol; // http://

So, you will probably need:

if (window.location.pathname === '/') {
    category = 'home';
    pagetype = 'homepage';
}

or if you are using ES6 you can also use:

const { pathname } = window.location;

if (pathname === '/') {
    category = 'home';
    pagetype = 'homepage';
}
Priya
  • 1,359
  • 6
  • 21
  • 41
Nicholas Robinson
  • 1,359
  • 1
  • 9
  • 20