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

157
Visualizações
Javascript causing some links not to work

I'm having an issue with this piece of JavaScript. The issue is that when it is enabled, some of my links aren't working. The links that open a webpage work fine, it's the mailto: and tel: links that won't work. Can someone please help me fix this issue? Thank you.

const allLinks = document.querySelectorAll("a:link");

allLinks.forEach(function (link) {
  link.addEventListener("click", function (e) {
    
    // conditional for preventDefault
    if (link.hasAttribute("target") === false) {
        e.preventDefault();
    } else {
      if (link.getAttribute("target") !== "_blank") {
        e.preventDefault();
      }
    }

    const href = link.getAttribute("href");

    // Scroll back to top
    if (href === "#")
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code is preventing the link from being followed if it doesn't have a target attribute set to "_blank".

Tel and mailto links don't usually have a target. As @The Fool mentioned, this looks suspicious and it's unclear how you expected it to work.

You can probably fix it by adding the following to the top of your handler.

if (link.href.startsWith("tel:")  || link.href.startsWith("mailto:")) {
    return
}
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