位置:href 屬性

Baseline 已廣泛支援

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

Location 介面的 href 屬性是一個 stringifier,它返回一個包含完整 URL 的字串,並允許更新 href。

設定 href 的值會導航到所提供的 URL。如果你想要重定向,請使用 location.replace()。與設定 href 屬性值的區別在於,當使用 location.replace() 方法時,在導航到給定的 URL 後,當前頁面將不會儲存在會話 歷史記錄 中——這意味著使用者將無法使用後退按鈕導航回該頁面。

字串。

示例

js
// Lets imagine an <a id="myAnchor" href="https://mdn.club.tw/en-US/Location/href"> element is in the document
const anchor = document.getElementById("myAnchor");
const result = anchor.href; // Returns: 'https://mdn.club.tw/en-US/Location/href'

規範

規範
HTML
# dom-location-href-dev

瀏覽器相容性