Fire an event when an image in the generated content is loaded


Melekus

I have a question about some javascript generated content:

I use the onload()html page 's events to load HTML code from the server and inject it into the page. Since it's already html, I use the attributes of innerHtmlthe node I want to populate with the content .

After injecting the HTML, I call a handcrafted adjustHeights()function that normalizes the heights of elements created this way so that they all match the height of the tallest element.

All of this works perfectly unless the code in it innerHtmlcontains some kind of image or other media that takes time to load (especially video), since adjustHeights()the call is made between the code injection and the end of load time for the image/video/etc. code.

Is there a way to wait for each element to load before calling it adjustHeights()? I've tried the document.onreadystatechangeproperty, but when I start injecting the code, the state is already "completed".

I'd rather not use time based calls if possible adjustHeights().

To make it more clear, here is an example:

var mylist = document.getElementById('mycontent');
var li;
for (var i = 0; i < res.length; i++)
{
    li = document.create('li');
    li.innerHTML=res[i];
    mylist.appendChild(li);
}
adjustHeights();
roxxypoxxy

For images and other types of media, the "onload" event should be used so that Adjust height can be called later. example:

<img src="someimage.png" onload="loadImage()" >

<script>
   function loadImage() {
     // code to adjust heights
    }
</script>

Here is the standard version:

var img = document.querySelector('img')

function loaded() {
  alert('loaded');
  // do something to adjust height
}

if (img.complete) {
  loaded()
} else {
  img.addEventListener('load', loaded)
  img.addEventListener('error', function() {
      alert('error')
  })
}

Related


Fire an event when an image in the generated content is loaded

Melekus I have a question about some javascript generated content: I use the onload()html page 's events to load HTML code from the server and inject it into the page. Since it's already html, I use the attributes of innerHtmlthe node I want to populate with t

Fire an event when an image in the generated content is loaded

Melekus I have a question about some javascript generated content: I use the onload()html page 's events to load HTML code from the server and inject it into the page. Since it's already html, I use the attributes of innerHtmlthe node I want to populate with t

Fire an event when an image in the generated content is loaded

Melekus I have a question about some javascript generated content: I use the onload()html page 's events to load HTML code from the server and inject it into the page. Since it's already html, I use the attributes of innerHtmlthe node I want to populate with t

Fire an event when an image in the generated content is loaded

Melekus I have a question about some javascript generated content: I use the onload()html page 's events to load HTML code from the server and inject it into the page. Since it's already html, I use the attributes of innerHtmlthe node I want to populate with t

Fire an event when an image in the generated content is loaded

Melekus I have a question about some javascript generated content: I use the onload()html page 's events to load HTML code from the server and inject it into the page. Since it's already html, I use the attributes of innerHtmlthe node I want to populate with t

Fire an event when asynchronous JavaScript is loaded

Jonah I'm changing external javascript resources to load asynchronously to speed up page loading. For this I add the async attribute to the script tag: <script async type="text/javascript" src="external.js"></script> I've included the following code to execut

Fire an event when asynchronous JavaScript is loaded

Jonah I'm changing external javascript resources to load asynchronously to speed up page loading. For this I add the async attribute to the script tag: <script async type="text/javascript" src="external.js"></script> I've included the following code to execut

Fire an event when asynchronous JavaScript is loaded

Jonah I'm changing external javascript resources to load asynchronously to speed up page loading. For this I add the async attribute to the script tag: <script async type="text/javascript" src="external.js"></script> I've included the following code to execut

Fire an event when asynchronous JavaScript is loaded

Jonah I'm changing external javascript resources to load asynchronously to speed up page loading. For this I add the async attribute to the script tag: <script async type="text/javascript" src="external.js"></script> I've included the following code to execut

jQuery fire event when specific element is loaded

Ch4rAss I want to capture globally whenever some element (fe ) textareais rendered on the page to process it . Elements can also be inserted via AJAX requests. // This is just an example of functionality I want to achieve // Not a real code $(document).on('ren

Fire an event when asynchronous JavaScript is loaded

Jonah I'm changing external javascript resources to load asynchronously to speed up page loading. For this I add the async attribute to the script tag: <script async type="text/javascript" src="external.js"></script> I've included the following code to execut

Fire an event when asynchronous JavaScript is loaded

Jonah I'm changing external javascript resources to load asynchronously to speed up page loading. For this I add the async attribute to the script tag: <script async type="text/javascript" src="external.js"></script> I've included the following code to execut

jQuery fire event when specific element is loaded

Ch4rAss I want to capture globally whenever some element (fe ) textareais rendered on the page to process it . Elements can also be inserted via AJAX requests. // This is just an example of functionality I want to achieve // Not a real code $(document).on('ren

NextJS: Image loaded from cache doesn't fire onLoad event

Mohammad Saif Khalid I'm currently converting a ReactJS client-side rendered app to a NextJS app for SEO and social media linking reasons. One of the converted components (basically an image that waits for it to load and then fades away) doesn't work properly

NextJS: Image loaded from cache doesn't fire onLoad event

Mohammad Saif Khalid I'm currently converting a ReactJS client-side rendered app to a NextJS app for SEO and social media linking reasons. One of the transformed components (basically an image that waits for it to load and then fades away) doesn't work properl

NextJS: Image loaded from cache doesn't fire onLoad event

Mohammad Saif Khalid I'm currently converting a ReactJS client-side rendered app to a NextJS app for SEO and social media linking reasons. One of the transformed components (basically an image that waits for it to load and then fades away) doesn't work properl

NextJS: Image loaded from cache doesn't fire onLoad event

Mohammad Saif Khalid I'm currently converting a ReactJS client-side rendered app to a NextJS app for SEO and social media linking reasons. One of the transformed components (basically an image that waits for it to load and then fades away) doesn't work properl

NextJS: Image loaded from cache doesn't fire onLoad event

Mohammad Saif Khalid I'm currently converting a ReactJS client-side rendered app to a NextJS app for SEO and social media linking reasons. One of the transformed components (basically an image that waits for it to load and then fades away) doesn't work properl

How to fire event when QListWidget has added or removed content

nwfistere When adding an element to a QListWidget, I want to enable or disable an action depending on whether there is an element in the QListWidget. Is there any other list other listWidget.itemChanged.connect(self.checkListLength)than the varying number of e

How to fire event when QListWidget has added or removed content

nwfistere When adding an element to a QListWidget, I want to enable or disable an action depending on whether there is an element in the QListWidget. Is there any other list other listWidget.itemChanged.connect(self.checkListLength)than the varying number of e

How to fire event when QListWidget has added or removed content

nwfistere When adding an element to a QListWidget, I want to enable or disable an action depending on whether there is an element in the QListWidget. Is there any other list other listWidget.itemChanged.connect(self.checkListLength)than the varying number of e