JavaScript ने आवश्यक लिंक के href एट्रिब्यूट को प्राप्त करने के लिए document.links.href प्रदान किया है। यह मेथड उसी तरह काम करता है जैसे ऐरे में एक विशेष एलिमेंट प्रदर्शित करने वाला मेथड। आइए इसे संक्षेप में चर्चा करें।
उदाहरण-1
निम्न उदाहरण में, तीन लिंक प्रदान किए गए थे। document.links.href का उपयोग करके तीसरा लिंक आउटपुट में निष्पादित होता है। यह ऐरे के समान ही अनुसरण करता है। पहला लिंक 0वें इंडेक्स लेगा, दूसरा लिंक 1ला इंडेक्स लेगा और इसी तरह।
<html> <body> <a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br> <a href="https://www.tutorialspoint.com/java8/index.htm">Java</a><br> <a href="https://www.tutorialspoint.com/spring/index.htm">Spring</a> <p id="links"></p> <script> document.getElementById("links").innerHTML = "The href attribute is: " + document.links[2].href; </script> </body> </html>
आउटपुट
Javascript Java Spring The href attribute is: https://www.tutorialspoint.com/spring/index.htm
उदाहरण-2
निम्न उदाहरण में, दो लिंक प्रदान किए गए थे। document.links.href का उपयोग करके पहला लिंक आउटपुट में निष्पादित होता है।
<html> <body> <a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br> <a href="https://www.tutorialspoint.com/java8/index.htm">Java</a>; <p id="links"></p> <script> document.getElementById("links").innerHTML = "The href attribute is: " + document.links[0].href; </script> </body> </html>
आउटपुट
Javascript Java The href attribute is: https://www.tutorialspoint.com/javascript/index.htm