Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have iframe load when visible

I have a page with about 100 videos on them (youtube) loading an iframe. They are all in an accordion style menu, I was wondering if there was a way to have them load when the accordion is open or the iframe is visible on the page. Right now it takes up way too much memory and lags the page a lot.

like image 871
Nurdism Avatar asked Oct 20 '13 20:10

Nurdism


People also ask

How do I load an iframe in the background?

visibility ='hidden' now the iframe is in hidden mode, then set ifrm. style. visibility ='visible' now iframe will be visible to you. you can test this by using browser's inspect element.

Can you lazy load iframe?

Can I lazy-load iframes cross-browser? Yes # iframe lazy-loading can be applied as a progressive enhancement. Browsers which support loading=lazy on iframes will lazy-load the iframe, while the loading attribute will be safely ignored in browsers which do not support it yet.

Does iframe block page load?

Whereas iframes are typically used to include one HTML page within another, the Script in Iframe technique leverages them to load JavaScript without blocking, as shown by the Script in Iframe example.

Do iframes slow down page load?

So, you should not use iframe excessively without monitoring what's going on, or you might end up harming your page performance. To avoid having your iframes slow down your pages, a good technique is to lazy load them (i.e., loading them only when they are required like when the user scrolls near them).


2 Answers

You haven't shown any code that allows us to include this in your code.

Don't set the src of the video, store it in data

<iframe src="about:blank" data-src="http://youtu.be/etc"></iframe>

Using whatever triggers the accordion, $(this) being that accordion item

var $iframe=$(this).find('iframe');
if ($iframe.data('src')){ // only do it once per iframe
    $iframe.prop('src', $iframe.data('src')).data('src', false);
}
like image 58
Popnoodles Avatar answered Oct 12 '22 07:10

Popnoodles


Okay so it will go as follow:

HTML

<div id="accordion">
    <h3>Video 1</h3>
    <div class="content" data-link="//www.youtube.com/embed/nlcIKh6sBtc">
        <iframe width="200" height="200" src="" frameborder="0"></iframe>
    </div>

    <h3>Video 2</h3>
    <div class="content" data-link="//www.youtube.com/embed/My2FRPA3Gf8">
        <iframe width="200" height="200" src="" frameborder="0"></iframe>
    </div>

    <h3>Video 3</h3>
    <div class="content" data-link="//www.youtube.com/embed/CevxZvSJLk8">
        <iframe width="200" height="200" src="" frameborder="0"></iframe>
    </div>
</div>

javascript

var elements = document.getElementsByClassName("content"), bubbles = false;

var observer = new WebKitMutationObserver(function (mutations) {
  mutations.forEach(attrModified);
});

for(var i = 0; i < elements.length; i++){
    observer.observe(elements[i], { attributes: true, subtree: bubbles });
}

function attrModified(mutation) {
    var contentStyle = mutation.target.getAttribute("style");
    var IsVisible = contentStyle.indexOf("block") != -1;
    if(!IsVisible){
        var $currentIframe = $(mutation.target).children("iframe");
        $currentIframe.attr("src", "");
    }
    else{
        var link = $(mutation.target).data("link");
        var $currentIframe = $(mutation.target).children("iframe");
        $currentIframe.attr("src", link);
    }
}

$("#accordion").accordion();

Example: AccordionLoadFrame

like image 42
Ibrahim Ahmed Avatar answered Oct 12 '22 06:10

Ibrahim Ahmed