HTMLImageElement.decode()

The decode() method of the HTMLImageElement interface returns a Promise that resolves when the image is decoded and it is safe to append the image to the DOM. This prevents a decoding delay on the next frame.

Syntax

var promise = HTMLImageElement.decode();

Parameters

None.

Return value

A Promise which is resolved once the image data is ready to be used.

Exceptions

EncodingError
A DOMException indicating that an error occurred while decoding the image.

Usage notes

One potential use case for decode(): when loading very large images (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 new HTMLImageElement, setting its source to the full-resolution image's URL, then using decode() 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.

Examples

The following example shows how to use the decode() method to control when an image is appended to the DOM. Without a Promise-returning method, you would add the image to the DOM in a load event handler, such as by using the img.onload event handler, and by handling the error in the error event's handler.

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

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'decode()' in that specification.
Living Standard Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 64Edge ? Firefox No support NoIE ? Opera ? Safari ? WebView Android Full support 64Chrome Android Full support 64Edge Mobile ? Firefox Android No support NoOpera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

Document Tags and Contributors

Contributors to this page: mdnwebdocs-bot, jpmedley, Sheppy
Last updated by: mdnwebdocs-bot,