Search code examples
javascriptgoogle-chromeanchorhrefrelative-path

How to get the relative path from a a.href property in Chrome's JavaScript?


If <a href="{relative path}">...</a> is specified in a page's HTML the a.href property in Chrome's JavaScript returns an absolute path with the current document.URL.

I know I can extract the relative path portion from there with a combination of replace(), substr(), indexOf(), but I'd like to avoid this. Is there a property/function that returns exactly that what the HTML page developer defined as {relative path}?


Solution

  • getAttribute("href")

    console.log(document.getElementById("a").getAttribute("href"))
    <a id="a" href="/pages/index.html">Link</a>