HTMLAnchorElement: origin 屬性

Baseline 已廣泛支援

此功能已成熟,可跨多種裝置和瀏覽器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有瀏覽器中可用。

HTMLAnchorElement 介面的只讀屬性 origin 返回一個字串,其中包含 <a> 元素的 href 的 origin 的 Unicode 序列化。

具體的結構因 URL 型別而異。

  • 對於使用 ftp:http:https:ws:wss: 方案的 URL,它會返回 protocol 後面跟著 //,再後面跟著 host。與 host 相同,port 僅在不為協議預設埠時包含。
  • 對於使用 file: 方案的 URL,其值取決於瀏覽器。
  • 對於使用 blob: 方案的 URL,返回的是 blob: 後面的 URL 的 origin,但前提是該 URL 使用 http:https:file: 方案。例如,blob:https://mozilla.org 將返回 https://mozilla.org

對於所有其他情況,返回字串 "null"

有關更多資訊,請參閱 URL.origin

字串。

示例

js
// An <a id="myAnchor" href="https://mdn.club.tw/en-US/HTMLAnchorElement"> element is in the document
const anchor = document.getElementById("myAnchor");
anchor.origin; // returns 'https://mdn.club.tw'

規範

規範
HTML
# dom-hyperlink-origin-dev

瀏覽器相容性

另見