JavaScript ने लिंक में href और target एट्रिब्यूट्स प्राप्त करने के लिए document.href और document.target प्रदान किए हैं। एक लिंक आमतौर पर <p> टैग में रखा जाता है और उसके अंदर एक एंकर टैग प्रदान किया जाता है, जिसमें href और target एट्रिब्यूट्स होते हैं। आइए उन्हें व्यक्तिगत रूप से चर्चा करें।
href एट्रिब्यूट ढूंढना
href एट्रिब्यूट प्राप्त करने के लिए, document.href का उपयोग किया जाना चाहिए। निम्न उदाहरण में, href एट्रिब्यूट में "https://www.tutorialspoint.com/" नामक एक लिंक होता है, जो प्रोग्राम निष्पादित होने पर प्रदर्शित होगा।
उदाहरण
<html> <body> <p><a id="myId" href="https://www.tutorialspoint.com/">tutorialspoint</a></p> <script> var ref = document.getElementById("myId").href; document.write(ref); </script> </body> </html>
आउटपुट
tutorialspoint https://www.tutorialspoint.com/
target एट्रिब्यूट ढूंढना
target एट्रिब्यूट प्राप्त करने के लिए, document.target का उपयोग किया जाना चाहिए। निम्न उदाहरण में, target एट्रिब्यूट "_union" प्रोग्राम निष्पादित होने पर प्रदर्शित हुआ था।
उदाहरण
<html> <body> <p><a id="myId" target="_union" href="https://www.tutorialspoint.com/">tutorialspoint</a></p> <script> var ref = document.getElementById("myId").target; document.write(ref); </script> </body> </html>
आउटपुट
tutorialspoint _union