Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lazy load iframe (delay src http call) with jquery

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

like image 359
Steve Avatar asked Aug 23 '11 00:08

Steve


2 Answers

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.

like image 124
alex Avatar answered Oct 21 '22 02:10

alex


A similar question was recently posted specific to iFrames with Twitter Bootstrap-based tabs, which I answered using a technique I'm calling Lazy Loading of iFrames. The js/jquery is as follows; see the full code including html markup in the fiddle or on my response to the OP:

<script>
$('#myTabs').bind('show', function(e) {  

// identify the tab-pane
paneID = $(e.target).attr('href');

// get the value of the custom data attribute to use as the iframe source
src = $(paneID).attr('data-src');

//if the iframe on the selected tab-pane hasn't been loaded yet...
if($(paneID+" iframe").attr("src")=="")
{
    // update the iframe src attribute using the custom data-attribute value
    $(paneID+" iframe").attr("src",src);
}
});
</script>
like image 40
technoTarek Avatar answered Oct 21 '22 02:10

technoTarek