The Image()
constructor creates a new HTMLImageElement
instance.
It is functionality equivalent to document.createElement('img')
.
Syntax
Image([unsigned long width, unsigned long height])
Parameters
- width
- The width of the image (i.e. the value for the
width
attribute). - height
- The height of the image (i.e. the value for the
height
attribute).
Examples
var myImage = new Image(100, 200); myImage.src = 'picture.jpg'; document.body.appendChild(myImage);
Would be the equivalent of defining the following HTML right inside the
<body>
:<img width="100" height="200" src="picture.jpg">
Note: The entire bitmap is loaded regardless of what size is specified in the constructor. If any size is specified in the constructor it will be reflected through the properties HTMLImageElement.width
and HTMLImageElement.height
of the resulting instance. The intrinsic width and height of the image in CSS pixels is reflected through the properties HTMLImageElement.naturalWidth
and HTMLImageElement.naturalHeight
. If no size is specified in the constructor both pairs of these properties will have the same values.
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'Image()' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Document Tags and Contributors
Tags:
Contributors to this page:
StevenXL,
chrisdavidmills,
K3N,
Thomas-Brierley,
klusark,
fscholz,
teoli,
Robg1,
jonfen,
kscarfone,
Abin_Abraham,
Sheppy,
Brettz9
Last updated by:
StevenXL,