Relative URL ใช้สำหรับลิงก์ไปยังหน้าอื่นในเว็บไซต์เดียวกัน ไม่ต้องระบุ domain เต็ม
รูปแบบ Relative URL
| รูปแบบ | ความหมาย | ตัวอย่าง |
/about | จาก root ของ domain | https://example.com/about |
about.html | ในโฟลเดอร์เดียวกัน | https://example.com/folder/about.html |
../about.html | ย้อนกลับ 1 โฟลเดอร์ | https://example.com/about.html |
../images/photo.jpg | ย้อน 1 โฟลเดอร์ เข้า images | - |
./contact.html | โฟลเดอร์ปัจจุบัน (ชัดเจน) | - |
Attribute ของ <a> tag
| Attribute | คำอธิบาย |
href | URL ปลายทาง (จำเป็น) |
rel | ความสัมพันธ์: nofollow, external, author, bookmark |
target | เป้าหมาย: _self, _blank, _parent, _top |
title | Tooltip เมื่อ hover |
download | บังคับดาวน์โหลด |
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<title>HTML Relative URLs</title>
</head>
<body>
<p>
Our <a href="/about/about_team.htm">Team</a> comprises of programmers, writers, and analysts.
</p>
<nav>
<a href="/products">Products</a> |
<a href="/services">Services</a> |
<a href="/contact">Contact</a>
</nav>
</body>
</html>
ข้อดีของ Relative URL
- ย้าย domain/host ง่าย (ไม่ต้องแก้โค้ด)
- โค้ดสั้น อ่านง่าย
- ทำงานได้ทั้ง localhost และ production