question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't load after dynamically inserted images

See original GitHub issue

Some browsers (including Safari for iOS) doesn’t recognize WebP image format. So, i want to use Modernizr lib to detect if i will deliver PNG/JPG or WebP.

The code to insert the new data-src works, but i want to use it with LOZAD, but, it doesn’t load that images dynamically inserted.

// * Lozad
const observer = lozad();
observer.observe();

// * jQuery
$(function() {

    // * Modernizr
    Modernizr.on('webp', function (result) {
        if(result) {
            $("img").each(function() {
                imgDelivery = $(this).data("webp");
                $(this).attr("data-src", imgDelivery);
            }); 
        }
        else {
            $("img").each(function() {
                imgDelivery = $(this).data("original");
                $(this).attr("data-src", imgDelivery);
            });
        }
        observer.observe(); // * Doesnt load the new images inserted above
    });
});

Tested on: Chrome, Safari for osx 12.0.02, Opera, Safari for iOS

testing

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
shivbabbarcommented, Nov 24, 2020
   jQuery("img").each(function(i, elm) {
        imgDeliverySrc = jQuery(this).attr("src");
        jQuery(this).attr("data-src", imgDeliverySrc);
        jQuery(this).addClass("lozad");
        jQuery(this).removeAttr("src");
   }).promise().done( function(){

    //alert("All was done");

    my_custom_run_lozad();

   });  // each loop completed




function my_custom_run_lozad()
{
    const observer = lozad();
    observer.observe();



    lozad('.lozad', {
        load: function(el) {
            console.log('loading IMAGE');
            // Custom implementation to load an element
            // e.g. el.src = el.getAttribute('data-src');
        }
    });
}

In this code, I run the lozad after attach data-src to each image after completed the jquery each Working fine for me.

1reaction
chesiocommented, Feb 21, 2019

Just a blind guess: add the lozad class dynamically (= when you set data-src attribute).

Based from presence of data-loaded attribute, I guess that Lozad marks the image as loaded (even if there is no src and thus there’s nothing to load) and does not load the image after you add data-src to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Load event for dynamically added images are not firing
Load event for images added dynamically to #div are not working, whereas other events like click/mouseover etc are working fine. <div id="test"> ...
Read more >
Loading image is not displaying on dynamically created HTML ...
I am dynamically creating HTML page to show User Information. I have added loading image to show the image until data loads properly....
Read more >
Page: DOMContentLoaded, load, beforeunload, unload
Scripts that are generated dynamically with document.createElement('script') and then added to the webpage also don't block this event.
Read more >
Preventing Content Reflow From Lazy-Loaded Images
Lazy loading doesn't guarantee that the image will fully load before it enters the viewport. The result is a perceived janky experience, ...
Read more >
Dynamically Loaded Images Won't Render - Ionic Forum
After upgrading to Ionic version 1.0.0-beta.14, my dynamically generated images no longer render. The image links are pulled in from a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found