HTML:
<a href="3.html">No. 3</a>JS:
$('a').attr('href') // would return the string "3.html"¿Cómo puedo obtener la cadena de la ruta completa?
$('a').fullLinkAttr('href') // returns https://www.example.com/3.htmlObtener el .href resuelve el problema fácilmente:
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>O como lo señaló Kaiido en los comentarios, se podría usar el constructor de URL:
const absolutePath = new URL("/absolute/or/relative/url", document.baseURI).href;Con jQuery:
const absolutePath = new URL($("a").attr("href"), document.baseURI).href;