Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Response
  4. redirected

Response: redirected property

Baseline Widely available

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

Note: This feature is available inWeb Workers.

Theredirected read-only property of theResponse interface indicates whether or not the response is the result of a request you made which was redirected.

Note:Checkingredirected to prevent redirects is not recommended, because by the time a response is received, the redirect has already happened, and you may have sent the request to an unintended destination, potentially sending sensitive information.Instead, you should do the filtering when you callfetch().See the exampleDisallowing redirects, which shows this being done.

Value

A boolean value which istrue if the response indicates that your request was redirected.

Examples

Detecting redirects

Checking to see if the response comes from a redirected request is as simple as checking this flag on theResponse object.In the code below, a textual message is inserted into an element when a redirect occurred during the fetch operation.Note, however, that this isn't as safe as outright rejecting redirects if they're unexpected, as described underDisallowing redirects below.

Theurl property returns the final URL after redirects.

js
fetch("awesome-picture.jpg")  .then((response) => {    const elem = document.getElementById("warning-message-box");    elem.textContent = response.redirected ? "Unexpected redirect" : "";    // final url obtained after redirects    console.log(response.url);    return response.blob();  })  .then((imageBlob) => {    const imgObjectURL = URL.createObjectURL(imageBlob);    document.getElementById("img-element-id").src = imgObjectURL;  });

Disallowing redirects

Checkingredirected is a bad way to prevent redirects, because the redirect has already happened. Instead, you should set the redirect mode to"error" in theoptions parameter when callingfetch(), like this:

js
fetch("awesome-picture.jpg", { redirect: "error" })  .then((response) => response.blob())  .then((imageBlob) => {    const imgObjectURL = URL.createObjectURL(imageBlob);    document.getElementById("img-element-id").src = imgObjectURL;  });

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp