Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีดึงค่าแอตทริบิวต์ rel ของลิงก์ใน JavaScript

หากต้องการดึงค่าแอตทริบิวต์ rel ของลิงก์ใน JavaScript ให้ใช้คุณสมบัติ rel แอตทริบิวต์ rel ใช้ระบุความสัมพันธ์ของหน้าปัจจุบันกับหน้าปลายทาง เช่น nofollow, noopener, noreferrer, canonical, alternate, stylesheet ฯลฯ

ตัวอย่าง

ลองรันโค้ดด้านล่างเพื่อดึงค่าแอตทริบิวต์ rel ของลิงก์:

<!DOCTYPE html>
<html>
  <body>
    <p><a id="anchorid" rel="nofollow" href="https://www.qries.com/">Qries</a></p>
    <script>
      var myVal = document.getElementById("anchorid").rel;
      document.write("Value of rel attribute: "+myVal);
    </script>
  </body>
</html>

ผลลัพธ์: "nofollow" คุณสมบัติ rel อ่าน/เขียนได้ (read/write) สามารถตั้งค่าได้: link.rel = "noopener noreferrer" ค่าที่ใช้บ่อย: nofollow (บอก Search Engine อย่าติดตาม), noopener (ป้องกัน window.opener), noreferrer (ไม่ส่ง Referer header)