জাভাস্ক্রিপ্ট-এ document.links.href রয়েছে যেটি প্রয়োজনীয় লিংকের href অ্যাট্রিবিউট বের করতে সাহায্য করে। এই মেথডটি অ্যারেতে একটি নির্দিষ্ট এলিমেন্ট দেখানোর মেথডের মতোই কাজ করে। সংক্ষেপে আলোচনা করা যাক।
উদাহরণ-১
নিচের উদাহরণে, তিনটি লিংক দেওয়া আছে। 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><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
উদাহরণ-২
নিচের উদাহরণে, দুটি লিংক দেওয়া আছে। 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