lazy load iframe (delay src http call) with jquery

Steve picture Steve · Aug 23, 2011 · Viewed 23.5k times · Source

I am looking for something similar to jQuery image lazy load plugin, but for iframes.

Answer

alex picture alex · Aug 23, 2011

This worked for me.

var iframes = $('iframe');

$('button').click(function() {
    iframes.attr('src', function() {
        return $(this).data('src');
    });
});

iframes.attr('data-src', function() {
    var src = $(this).attr('src');
    $(this).removeAttr('src');
    return src;
});

jsFiddle.