Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In 1.x-dev lazy loading adds an almost empty div with "Loading..." and adds JS separately. Somehow that doesn't work well with anonymous page caching (or block caching?), because the empty block is cached, but not the required JS.
Wrong:
$lazy_load = '<div class="throbber twitter-pull-lazy" id="' . $id . '">' . t('Loading...') . '</div>';
drupal_add_js('jQuery(document).ready(function () { jQuery.get("' . $uri . '", function(data) { jQuery("#'. $id . '").html(data).removeClass("throbber"); }); });', 'inline');
Right:
$script = 'jQuery(document).ready(function () { jQuery.get("' . $uri . '", function(data) { jQuery("#'. $id . '").html(data).removeClass("throbber"); }); });';
$lazy_load = '<div class="throbber twitter-pull-lazy" id="' . $id . '">' . t('Loading...') . '<script>' . $script . '</script></div>';
That way the inline JS is cached together with the empty block. Separation of markup and code isn't always good.
I've no idea how this works in 2.x, so I might be waking up dead cows (or whatever, you know).
Comments
Comment #1
rudiedirkx CreditAttribution: rudiedirkx commented