Get local href value from anchor (a) tag
The below code gets the full path, where the anchor points:
document.getElementById("aaa").href; // http://example.com/sec/IF00.html
while the one below gets the value of the href
attribute:
document.getElementById("aaa").getAttribute("href"); // sec/IF00.html
document.getElementById("link").getAttribute("href");
If you have more than one <a>
tag, for example:
<ul> <li> <a href="1"></a> </li> <li> <a href="2"></a> </li> <li> <a href="3"></a> </li></ul>
You can do it like this: document.getElementById("link")[0].getAttribute("href");
to access the first array of <a>
tags, or depends on the condition you make.
This code works for me to get all links of the document
var links=document.getElementsByTagName('a'), hrefs = [];for (var i = 0; i<links.length; i++){ hrefs.push(links[i].href);}