文件:location 屬性
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 |
瀏覽器相容性
載入中…
另見
- 返回值的介面,
Location - 類似的資訊,但附加到 瀏覽上下文,
Window.location