URL:origin 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

We’d love to hear your thoughts on the next set of proposals for the JavaScript language. You can find a description of the proposals here.
Please take two minutes to fill out our short survey.

备注: 此特性在 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
# dom-url-origin

浏览器兼容性

参见