Window location.href
示例
獲取當前頁面的 URL
let url = location.href;
自己動手試一試 »
設定當前頁面的 URL
location.href = "https://w3schools.tw";
自己動手試一試 »
更多示例見下文。
描述
location.href
屬性設定或返回當前頁面的整個 URL。
語法
返回 href 屬性
location.href
設定 href 屬性
location.href = URL
屬性值
引數 | 描述 |
URL | 一個絕對 URL,例如 http://www.example.com/default.htm 一個相對 URL,例如 default.htm 一個錨點 URL,例如 location.href="#top" 一個新協議,例如 ftp://someftpserver.com mailto:someone@example.com file://host/path/example.txt |
返回值
型別 | 描述 |
一個字串 | 頁面的完整 URL,包括協議(如 https://)。 |
更多示例
瀏覽器支援
location.href
在所有瀏覽器中都受支援
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 是 |