URL:origin 属性

备注: 此特性在 Web Worker 中可用。

URL 接口的 origin 只读属性返回一个包含所表示 URL 的来源(origin)的 Unicode 序列化形式的字符串。

具体结构因 URL 类型而异:

  • 对于 httphttps URL,其格式为:协议名紧接 '://',随后是域名,紧接着是 ':',之后是端口号(如果明确指定且非默认端口——分别为 80443)。
  • 对于 file: URL,其值取决于浏览器。
  • 对于 blob: URL,将使用 blob: 后面的 URL 的来源。例如,"blob:https://mozilla.org" 将返回 "https://mozilla.org"

字符串

示例

js
const url = new URL("blob:https://mozilla.org:443/");
console.log(url.origin); // 输出“https://mozilla.org”

const url = new URL("http://localhost:80/");
console.log(url.origin); // 输出“http://localhost”

const url = new URL("https://mozilla.org:8080/");
console.log(url.origin); // 输出“https://mozilla.org:8080”

规范

Specification
URL Standard
# dom-url-origin

浏览器兼容性

BCD tables only load in the browser

参见