CKTIS / JavaScript
HTMLElement.onload — handling resource loading
HTMLElement.onload is the event-handler property for load. Assign a function to react to an element's resource loading, such as an image. A subsequent assignment replaces the previous function, while null disables that handler.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
element.onload = function (event) { /* handle load */ };
element.onload = null;HTMLElement.onload is the event-handler property for load, exposed through the GlobalEventHandlers mechanism. It is a browser interface defined by HTML, not a feature of ECMAScript itself; having Node.js 24 does not establish its availability.
For an image, load means the resource loaded successfully. Set the function before assigning src to avoid missing the event. Having the property on an element does not mean every element automatically emits load. The body and frameset elements are special: their onload handles the event on Window, rather than the loading of a separate element resource.
Assigning a new function replaces the previous one; null removes that handler. Use addEventListener('load', handler) for independent subscriptions. In the example, the image measures 1x1, and the function receives event. The ordinary function uses this to refer to the image.
Examples
(() => {
const image = document.createElement('img');
image.onload = function (event) {
console.log(event.type + ': ' + this.naturalWidth + 'x' + this.naturalHeight);
this.onload = null;
};
image.onerror = () => console.log('error');
image.src = 'data:image/svg+xml,' + encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>'
);
})();Result
load: 1x1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.