EN
JavaScript - Image() documentation with examples
0 points
The Image()
constructor lets us create objects representing HTML <img>
elements using JavaScript code in web browsers.
xxxxxxxxxx
1
// create image
2
var image = new Image();
3
image.src = 'https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png';
4
5
// append image in body
6
document.body.appendChild(image);
Syntax |
|
Typed syntax |
|
Parameters | width - 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 |
Description |
The |
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.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<script>
5
6
// create image
7
var image = new Image();
8
image.src = 'https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png';
9
10
// set lazy loading
11
image.loading = 'lazy';
12
13
// append image in body
14
document.body.appendChild(image);
15
16
</script>
17
</body>
18
</html>