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

174
Visualizações
What is the best way to get the absolute link of an <a> tag using JavaScript/jQuery?

HTML:

<a href="3.html">No. 3</a>

JS:

$('a').attr('href') // would return the string "3.html"

How can I get the string of the full path:

$('a').fullLinkAttr('href') // returns https://www.example.com/3.html
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Getting the .href solves the problem easy:

const a = document.getElementById("a"),
  b = document.getElementById("b"),
  c = document.getElementById("c");
  
console.log("A:", a.href);
console.log("B:", b.href);
console.log("C:", c.href);
<a href="/t/hello" id="a">A</a>
<a href="hello.html" id="b">B</a>
<a href="https://stackoverflow.com/testing..." id="c">C</a>

Or a pointed out by Kaiido in the comments one could use the URL constructor:

const absolutePath = new URL("/absolute/or/relative/url", document.baseURI).href;

With jQuery:

const absolutePath = new URL($("a").attr("href"), document.baseURI).href;
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