Javascript DOM ได้จัดเตรียมคุณสมบัติไว้มากมายเพื่อให้ได้รายละเอียดเกี่ยวกับเอกสาร ได้จัดเตรียมวิธีการที่เรียกว่า document.links.length เพื่อรับจำนวนลิงก์ในเอกสาร วิธีนี้จะไม่รบกวนการทำงานปกติของโค้ด
ตัวอย่าง-1
ในตัวอย่างต่อไปนี้ มีสามลิงก์ที่ให้ไว้ในโค้ด การใช้คุณสมบัติ DOM document.links.length จำนวนลิงก์จะแสดงในผลลัพธ์ตามที่แสดง
<html>
<body>
<a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br>
<a href="https://www.tutorialspoint.com/php/index.htm">Php</a><br>
<a href="https://www.tutorialspoint.com/java8/index.htm">Php</a><br>
<p id="links"></p>
<script>
document.getElementById("links").innerHTML =
"The number of links are: " + document.links.length;
</script>
</body>
</html> ผลลัพธ์
Javascript Php Java The number of links are: 3
ตัวอย่าง-2
ในตัวอย่างต่อไปนี้ มีสองลิงก์ที่ให้ไว้ในโค้ด การใช้คุณสมบัติ DOM document.links.length จำนวนลิงก์จะแสดงในผลลัพธ์ตามที่แสดง
<html>
<body>
<a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br>
<a href="https://www.tutorialspoint.com/bitcoin/index.htm">Bitcoin</a></br>
<script>
document.write("The number of links are: " + document.links.length);
</script>
</body>
</html> ผลลัพธ์
Javascript Bitcoin The number of links are: 2