window.location (read-only) trả về Location object chứa thông tin URL hiện tại của document.
Properties chính
| Property | Mô tả |
|---|---|
href | Full URL string. Gán giá trị → navigate. |
protocol | Scheme + ':' (https:) |
host | hostname + ':' + port |
hostname | Domain (không port) |
port | Port number |
pathname | Path (bắt đầu bằng /) |
search | Query string (bắt đầu bằng ?). Dùng URLSearchParams parse. |
hash | Fragment (bắt đầu bằng #) |
username, password | Credentials trong URL (hiếm dùng) |
Methods
location.assign(url): Navigate (thêm history).location.replace(url): Navigate (thay thế history entry).location.reload(): Reload page.location.toString(): Trảhref.
Ví dụ
// Current: https://example.com:8080/path?query=1#hash location.hostname // "example.com" location.pathname // "/path" location.search // "?query=1" location.hash // "#hash" location.port // "8080"