Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
url checker with includes()

i'm trying to make a url checker with js, this is the part of the code i'm having trouble with:

switch (true) {
    case document.location.pathname.includes("/account"):
      presenceData.details = "Viewing account..."
    break
    case document.location.pathname.includes("/mylist"):
      presenceData.details = "Viewing list..."
    break
    }
}

the url I am working with is {example.com}/account/profiles/mylist and when I test /mylist it keeps showing me "Viewing account..." what can I change to make /account not interfer /mylist?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem here is that {example.com}/account/profiles/mylist contains both the strings "/account" and "/mylist". So when you hit your first case, a match is made, and then you break out of the switch.

let pathname = '/account/profiles/mylist';
switch (true) {
  case pathname.includes("/account"):
    console.log("Viewing account...");
    break;
  case pathname.includes("/mylist"):
    console.log("Viewing list...");
    break;
}

If you know that /mylist is always going to be deeper in the hierarchy, beneath /account, you could just switch the order of the cases:

let pathname = '/account/profiles/mylist';
switch (true) {
  case pathname.includes("/mylist"):
    console.log("Viewing list...");
    break;
  case pathname.includes("/account"):
    console.log("Viewing account...");
    break;
}

Otherwise, you may need a more nuanced logic approach, and you may wish to avoid using switch statements in favor of if/else statements, as there are some peculiarities to a switch (specifically, that once a single case is met all other case blocks will be interpreted as matching until you break).

EDIT:

OR, another alternative would be to leverage endsWith instead:

let pathname = '/account/profiles/mylist';
switch (true) {
  case pathname.endsWith("/account"):
    console.log("Viewing account...");
    break;
  case pathname.endsWith("/mylist"):
    console.log("Viewing list...");
    break;
}

FULL DISCLOSURE -- as I was typing this edit esqew posted the same solution.

about 4 years ago · Juan Pablo Isaza Relatório

0

Why not use endsWith() instead...?

switch (true) {
    case document.location.pathname.endsWith("/account"):
      presenceData.details = "Viewing account..."
    break
    case document.location.pathname.endsWith("/mylist"):
      presenceData.details = "Viewing list..."
    break
    }
}

Tangential, but switch statements are (IMO) outdated and become difficult to read/maintain (considering the necessity of breaking every condition); additionally, your use of switch (true) is somewhat abusing what a switch is meant to accomplish. You probably should be simply using if/else if statements anyway:

if (document.location.pathname.endsWith("/account")) {
    presenceData.details = "Viewing account...";
}
else (document.location.pathname.endsWith("/mylist")) {
    presenceData.details = "Viewing list...";
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda