How to wait for a file (eg. "sunny.png") and send an alert, when it’s loaded using JavaScript.
My original intention was to just wait for the whole page to load, but DOMContentLoaded and document.readyState were all very early, with window.onload the page was still loaded and the event didn’t fire. I have to wait for the last file to load. I don’t have access to edit the original page.
2
Answers
You can use the Image object in JavaScript to load an image and then use the onload event to trigger an alert when the image is fully loaded. Here’s an example:
In this example, img.onload is a function that will be called when the image is fully loaded. The alert function will then display an alert message saying "Image loaded!".
You can simply use onload function to check this.
Onload in HTML,
In JavaScript:
In JavaScript, using the addEventListener() method:
For more reference: mdn,w3school