文件:location 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

Document.location 只讀屬性返回一個 Location 物件,該物件包含文件 URL 的資訊,並提供更改該 URL 和載入其他 URL 的方法。

儘管 Document.location 是一個只讀Location 物件,但您也可以為其分配一個字串。這意味著在大多數情況下,您可以像處理字串一樣處理 document.location:document.location = 'http://www.example.com'document.location.href = 'http://www.example.com' 的同義詞。如果您為其分配另一個字串,瀏覽器將載入您分配的網站。

要僅以字串形式檢索 URL,還可以使用只讀的 document.URL 屬性。

如果當前文件不在瀏覽上下文中,則返回值為 null

一個 Location 物件。

示例

js
console.log(document.location);
// Prints a Location object to the console

規範

規範
HTML
# the-location-interface

瀏覽器相容性

另見