Languages
[Edit]
EN

JavaScript - Image() documentation with examples

0 points
Created by:
Fletcher-Peralta
778

The Image() constructor lets us create objects representing HTML <img> elements using JavaScript code in web browsers.

 

Syntax

new Image()

new Image(width)

new Image(width, height)

Typed syntax

new Image(width?: number, height?: number): HTMLImageElement;

Parameterswidth - the width of the new image element (used as width attribute),
height - the height of the new image element (used as height attribute).
Result

The constructor returns a new HTMLImageElement object representing an HTML <img> element which is not attached to any DOM tree. When called without parameters, new Image() is equivalent to the document.createElement('img') method call.

Description

The Image() constructor lets us create a new HTMLImageElement instance, that represents an HTML <img>element.


Practical example

Edit

In this example, we use the Image() constructor to create an image element, then we set its src and append in body element. Additionally, we set a lazy loading for the image.

See also

Edit
  1. JavaScript - change image src

  2. JavaScript - preload image

  3. JavaScript - change image on click

  4. JavaScript - reload image with the same URL

  5. JavaScript - read image from clipboard as Data URLs encoded with Base64

References

Edit
  1. Image() - Web APIs | MDN
  2. Lazy loading - Web Performance | MDN

Alternative titles

  1. js - Image() documentation with examples
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join