Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. HTMLImageElement
  4. decode()

HTMLImageElement: decode() method

Baseline Widely available

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

Thedecode() method of theHTMLImageElement interface returns aPromise that resolves once the image is decoded and is safe to be appended to the DOM.

This can be used to initiate loading of the image prior to attaching it to an element in the DOM (or adding it to the DOM as a new element), so that the image can be rendered immediately upon being added to the DOM. This, in turn, prevents the rendering of the next frame after adding the image to the DOM from causing a delay while the image loads.

Syntax

js
decode()

Parameters

None.

Return value

APromise that fulfills withundefined once the image data is ready to be used.

Exceptions

EncodingErrorDOMException

An error occurred while decoding the image. This can happen if:

  • The request failed
  • The image request changed after callingdecode() (for example, by changing itssrc)
  • The image's data is corrupted

Examples

Basic usage

The following example shows how to use thedecode() method to control whenan image is appended to the DOM.

js
const img = new Image();img.src = "nebula.jpg";img  .decode()  .then(() => {    document.body.appendChild(img);  })  .catch((encodingError) => {    // Do something with the error.  });

Note:Without aPromise-returning method, you would add the image to the DOM in aload event handler, and handle the error in theerror event's handler.

Avoiding empty images

In the below example, you'll likely get an empty image shown on the page as the image is downloaded:

js
const img = new Image();img.src = "img/logo.png";document.body.appendChild(img);

Usingdecode() will delay inserting the image into the DOM until it is fully downloaded and decoded, thereby avoiding the empty image problem:

js
async function getImage() {  const img = new Image();  img.src = "img/logo.png";  await img.decode();  document.body.appendChild(img);  const p = document.createElement("p");  p.textContent = "Image is fully loaded!";  document.body.appendChild(p);}

This is particularly useful if you're dynamically swapping an existing image for a new one, and also prevents unrelated paints outside of this code from being held up while the image is decoding. For example, in an online photo album, you can present a low resolution thumbnail image initially and then replace that image with the full-resolution image by instantiating a newHTMLImageElement, setting its source to the full-resolution image's URL, then usingdecode() to get a promise which is resolved once the full-resolution image is ready for use. At that time, you can then replace the low-resolution image with the full-resolution one that's now available.

Specifications

Specification
HTML
# dom-img-decode-dev

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp