jetpack icon indicating copy to clipboard operation
jetpack copied to clipboard

Lazy Images: better support for content loaded via AJAX

Open jeherve opened this issue 8 years ago • 5 comments

Whenever I use AJAX to load content from another page of my site, only the 1×1 placeholder comes in. How do I reinitialize JetPack’s lazy images to pop the proper image back in after Ajax?

Just a basic jQuery load, loading HTML from one page into the current one. -- https://wordpress.org/support/topic/using-ajax-only-loads-1x1-lazy-placeholder/

For now, here is a work around:

function update_thumbs() {
    $('YOUR IMAGE SELECTOR').each(function() {
      var src = $(this).prop('src');
      if (src.indexOf('1x1.trans.gif') !== -1) {
        $(this).prop('src', $(this).data('orig-file'));
      }
    });
  }

jeherve avatar Mar 13 '18 10:03 jeherve

This issue has been marked as stale. This happened because:

  • It has been inactive in the past 6 months.
  • It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`.

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

stale[bot] avatar Sep 09 '18 10:09 stale[bot]

An alternative would be to use the following after loading the content via AJAX:

$(document.body).trigger('post-load');

This triggers a jQuery event that the lazy load module is listening for in order to lazy load images.

You could also add the 'skip-lazy' CSS class to the AJAX loaded content.

benjamin-t-gray avatar Oct 08 '19 13:10 benjamin-t-gray

This issue has been marked as stale. This happened because:

  • It has been inactive in the past 6 months.
  • It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`.

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

stale[bot] avatar Apr 05 '20 14:04 stale[bot]

Another possible report in https://wordpress.org/support/topic/lazy-load-images-no-longer-work-with-ajax/

I've suggested the workaround above to see if that helps.

KokkieH avatar Apr 29 '20 09:04 KokkieH

Best option will be disabling lazy load for ajax calls, add following code in function.php

function shankarg_dev_lazyload_exclude() {
    if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ){
        return false;
    }
    return true;
}
add_filter( 'lazyload_is_enabled', 'shankarg_dev_lazyload_exclude', 15 );

Jetpack Lazy Images doc https://jetpack.com/support/lazy-images/

You can also check for ajax request using

if(wp_doing_ajax()) {
    return false;
}

but in my case wp_doing_ajax() is not working. so I check with request header.

shankarGdev avatar Apr 16 '21 16:04 shankarGdev