Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!