Document links Collection - Loop through all links in the document, and output the URL (href) of each link: - Javascript DOM

Javascript examples for DOM:Document links

Description

Document links Collection - Loop through all links in the document, and output the URL (href) of each link:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<img src ="" width="145" height="126" alt="" usemap ="#myMap">

<map name="planetmap">
  <area shape="rect" coords="0,0,80,130" href="" alt="">
  <area shape="circle" coords="90,60,5" href="" alt="">
  <area shape="circle" coords="120,60,10" href="" alt="">
</map>/*  w w  w. j  av  a 2s  . co  m*/

<p>
  <a href="/html/default.asp">HTML</a><br>
  <a href="/css/default.asp">CSS</a>
</p>

<button type="button" onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.links;
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        txt = txt +  x[i].href + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials