Response:url 属性

Baseline Widely available

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

Response 接口的只读属性 url 包含了响应的 URL 地址。url 属性值为经过重定向后最终获得的 URL 地址。

一个字符串。

示例

Fetch Response 示例 中(请参见 Fetch Response 实时演示),我们使用 Request() 构造函数创建了一个新的 Request 对象,并向其传递了 JPG 的路径。然后,我们使用 fetch() 获取该请求,使用 Response.blob 从响应中提取一个 blob,使用 URL.createObjectURL 从中创建一个对象 URL,并将其显示在 <img> 中。

请注意,在 fetch() 块的顶部,我们会将响应 URL 记录到控制台。

js
const myImage = document.querySelector("img");

const myRequest = new Request("flowers.jpg");

fetch(myRequest)
  .then((response) => {
    console.log("response.url =", response.url); // response.url = https://mdn.github.io/dom-examples/fetch/fetch-response/flowers.jpg
    return response.blob();
  })
  .then((myBlob) => {
    const objectURL = URL.createObjectURL(myBlob);
    myImage.src = objectURL;
  });

规范

Specification
Fetch
# ref-for-dom-response-url①

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
url

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见