จาวาสคริปต์ ได้จัดเตรียม document.href และ document.target เพื่อรับแอตทริบิวต์ href และเป้าหมายในลิงก์ ลิงก์มักจะอยู่ใน
กำลังค้นหาแอตทริบิวต์ href
หากต้องการรับแอตทริบิวต์ href document.href จะต้องใช้ ในตัวอย่างต่อไปนี้ href แอตทริบิวต์ประกอบด้วยลิงก์ที่เรียกว่า "http://www.tutorialspoint.com/" ซึ่งจะแสดงขึ้นเมื่อโปรแกรมทำงาน
ตัวอย่าง
<html>
<body>
<p><a id="myId" href="http://www.tutorialspoint.com/">tutorialspoint</a></p>
<script>
var ref = document.getElementById("myId").href;
document.write(ref);
</script>
</body>
</html> ผลลัพธ์
tutorialspoint http://www.tutorialspoint.com/
การค้นหาแอตทริบิวต์เป้าหมาย
เพื่อให้ได้ เป้าหมาย แอตทริบิวต์ document.target จะต้องใช้ ในตัวอย่างต่อไปนี้ แอตทริบิวต์เป้าหมาย "_union " แสดงขึ้นเมื่อโปรแกรมทำงาน
ตัวอย่าง
<html>
<body>
<p><a id="myId" target="_union" href="http://www.tutorialspoint.com/">tutorialspoint</a></p>
<script>
var ref = document.getElementById("myId").target;
document.write(ref);
</script>
</body>
</html> ผลลัพธ์
tutorialspoint _union