Vị trí cửa sổ.href
Ví dụ
Lấy URL của trang hiện tại:
let url = location.href;
Hãy tự mình thử »Đặt URL của trang hiện tại:
location.href = "https://www.example.com.vn";
Hãy tự mình thử »Thêm ví dụ dưới đây.
Sự miêu tả
Thuộc tính location.href
đặt hoặc trả về toàn bộ URL của trang hiện tại.
Cú pháp
Trả về thuộc tính href:
location.href
Đặt thuộc tính href:
location.href = URL
Giá trị tài sản
Parameter | Description |
URL |
An absolute URL like: https://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 |
Giá trị trả về
Kiểu | Sự miêu tả |
Một chuỗi | URL đầy đủ của trang, bao gồm cả giao thức (như https://). |
Thêm ví dụ
Đặt giá trị href để trỏ đến một địa chỉ email (sẽ mở và tạo một email mới):
location.href = "mailto: [email protected] ";
Hãy tự mình thử »Hỗ trợ trình duyệt
location.href
được hỗ trợ trong tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |