ตำแหน่งหน้าต่าง.href
ตัวอย่าง
รับ URL ของหน้าปัจจุบัน:
let url = location.href;
ตั้งค่า URL ของหน้าปัจจุบัน:
location.href = "https://www.w3schools.com";
ตัวอย่างเพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
คุณสมบัติlocation.href
ตั้งค่าหรือส่งคืน URL ทั้งหมดของหน้าปัจจุบัน
ไวยากรณ์
คืนคุณสมบัติ href:
location.href
ตั้งค่าคุณสมบัติ href:
location.href = URL
มูลค่าทรัพย์สิน
Parameter | Description |
URL |
An absolute URL like: http://www.example.com/default.htm A relative URL like default.htm An anchor URL like location.href="#top" A new protocol like: ftp://someftpserver.com mailto:[email protected] file://host/path/example.txt |
คืนมูลค่า
พิมพ์ | คำอธิบาย |
สตริง | URL แบบเต็มของหน้า รวมถึงโปรโตคอล (เช่น https://) |
ตัวอย่างเพิ่มเติม
ตั้งค่า href ให้ชี้ไปที่จุดยึดภายในหน้า:
location.href = "#top";
ตั้งค่า href ให้ชี้ไปที่ที่อยู่อีเมล (จะเปิดและสร้างข้อความอีเมลใหม่):
location.href = "mailto:[email protected]";
รองรับเบราว์เซอร์
location.href
รองรับในทุกเบราว์เซอร์:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |