> For the complete documentation index, see [llms.txt](https://edrus.gitbook.io/mt-it/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://edrus.gitbook.io/mt-it/2nd-month/week-6/dom-document-object-model/working-with-events/onload.md).

# onload

### The window’s load event

For the [`window`](https://www.javascripttutorial.net/javascript-bom/javascript-window/) object, the `load` event is fired when the whole webpage (HTML) has loaded fully, including all dependent resources, including JavaScript files, CSS files, and images.

To handle the `load` event, you register an event listener using the `addEventListener()` method:

```javascript
window.addEventListener('load', (event) => {
    console.log('The page has fully loaded');
});Code language: JavaScript (javascript)
```

Or use the `onload` property of the `window` object:

```javascript
window.onload = (event) => {
    console.log('The page has fully loaded');
};Code language: JavaScript (javascript)
```

If you maintain a legacy system, you may find that the `load` event handler is registered in of the body element of the HTML document, like this:

```xml
<!DOCTYPE html>
<html>
<head>
    <title>JS load Event Demo</title>
</head>
<body onload="console.log('Loaded!')">
</body>
</html>Code language: HTML, XML (xml)
```

It’s a good practice to use the `addEventListener()` method to assign the `onload` event handler whenever possible.

### The image’s load event

The `load` event also fires on images. To handle the `load` event on images, you use the `addEventListener()` method of the image elements.

The following example uses the `load` event handler to determine if an image, which exists in the DOM tree, has been completely loaded:

```xml
<!DOCTYPE html>
<html>
<head>
    <title>Image load Event Demo</title>
</head>
<body>
    <img id="logo">
    <script>
        let logo = document.querySelector('#logo');

        logo.addEventListener('load', (event) => {
            console.log('Logo has been loaded!');
        });

        logo.src = "logo.png";
    </script>
</body>
</html>
Code language: HTML, XML (xml)
```

You can assign an `onload` event handler directly using the `onload` attribute of the `<img>` element, like this:

```xml
<img id="logo" 
     src="logo.png" 
     onload="console.log('Logo loaded!')">
Code language: HTML, XML (xml)
```

If you create an image element dynamically, you can assign an `onload` event handler before setting the `src` property as follows:

```javascript
window.addEventListener('load' () => {
    let logo = document.createElement('img');
    // assign and onload event handler
    logo.addEventListener('load', (event) => {
        console.log('The logo has been loaded');
    });
    // add logo to the document
    document.body.appendChild(logo);
    logo.src = 'logo.png';
});
Code language: JavaScript (javascript)
```

How it works:

* First, create an image element after the document has been fully loaded by placing the code inside the event handler of the window’s load event.
* Second, assign the `onload` event handler to the image.
* Third, add the image to the document.
* Finally, assign an image URL to the `src` attribute. The image will be downloaded to the element as soon as the `src` property is set.

### The script’s load event

The `<script>` element also supports the `load` event slightly different from the standard ways. The script’s `load` event allows you to check if a JavaScript file has been completely loaded.

Unlike images, the web browser starts downloading JavaScript files only after the `src` property has been assigned and the `<script>` element has been added to the document.

The following code loads the `app.js` file after the page has been completely loaded. It assigns an `onload` event handler to check if the `app.js` has been fully loaded.

```javascript
window.addEventListener('load', checkJSLoaded)

function checkJSLoaded() {
    // create the script element
    let script = document.createElement('script');
    
    // assign an onload event handler
    script.addEventListener('load', (event) => {
        console.log('app.js file has been loaded');
    });

    // load the script file
    script.src = 'app.js';
    document.body.appendChild(script);
}Code language: JavaScript (javascript)
```

### Summary

* The `load` event occurs when the document has been completely loaded, including dependent resources like JavaScript files, CSS files, and images.
* The `<img>` and `<script>` elements also support the `load` event.
* Use the `addEventListener()` method to register an `onload` event handler.

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://edrus.gitbook.io/mt-it/2nd-month/week-6/dom-document-object-model/working-with-events/onload.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
