Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay load of iframe?

I know there are some tools and techniques for delaying the load of javascript, but I have an iframe that I would like to delay loading until after the rest of the page has finished downloading and rendering (the iframe is in a hidden that will not be revealed until someone clicks on a particular tab on the page. Is there a way to delay the load of an iframe? Any suggestions would be much appreciated. Thanks!

like image 246
Sean Avatar asked Oct 19 '09 08:10

Sean


People also ask

How do I defer an iframe?

with jquery it is easy! either enclose your code which loads the iframe within a $() or use $(document). ready(function(){}) these both are the same and would execute your code after the DOM is ready!

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).

Do iframes load asynchronously?

Show activity on this post. One problem we had was that while iframes already do load asynchronously, the main page will not fire OnLoad until the iframe has finished loading too.


8 Answers

with jquery it is easy!

either enclose your code which loads the iframe within a $() or use $(document).ready(function(){}) these both are the same and would execute your code after the DOM is ready!

e.g.

$(document).ready(function(){
    $('iframe#iframe_id').attr('src', 'iframe_url');    
});

see more at http://www.learningjquery.com/2006/09/introducing-document-ready

like image 90
Tzury Bar Yochay Avatar answered Oct 18 '22 01:10

Tzury Bar Yochay


Don't know if need do run without javascript. But the best method is to change the src direct after the iframe:

<iframe id="myIframe" src="http://.." />
<script type="text/javascript">
  var iframe = document.getElementById('myIframe').src = iframe.src;
  iframe.src = '';
  document.onload =  function(){iframe.src = src;}
</script>

Using $(document).ready will start the rendering of your Iframe direct after the DOM Tree is build, but before all of the content in your side is loaded, so I think this isn't what you want.

jquery has the event .load, which is same as onload (after all resources are loaded)

$(window).load(function(){  iframe.src = src; }
like image 27
Andreas Köberle Avatar answered Oct 18 '22 01:10

Andreas Köberle


I don't understand why everyone is confusing JAVASCRIPT with JQUERY, but...

The pure JS solution is below: (basically it waits for the DOM to be built then loads all iframes in your page).

<iframe src="" data-src="YOUR ACTUAL iFRAME URL">
<script type="text/javascript">
      function load_iframes() {
var vidDefer = document.getElementsByTagName('iframe');
for (var i=0; i<vidDefer.length; i++) {
if(vidDefer[i].getAttribute('data-src')) {
vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));
} } }
      document.addEventListener("DOMContentLoaded", function(event) {
         load_iframes();
      });
    </script>

Note: Be careful about using the document.load event. Any resource that has a problem or has to spend 1 minute to load will stop your code from executing. This code snippet is tweaked (replaced load by domcontentloaded) from this reference.

like image 30
MarketerInCoderClothes Avatar answered Oct 18 '22 01:10

MarketerInCoderClothes


You can use the loading attribute to lazy load iframes.

<iframe src="https://example.com"
        loading="lazy"
        width="600"
        height="400"></iframe>
like image 21
iSWORD Avatar answered Oct 18 '22 02:10

iSWORD


Use Javascript in the onLoad event, or in the button click handler, to set the src attribute of the iframe.

like image 32
devio Avatar answered Oct 18 '22 02:10

devio


This works for me, but has problems if you change the code at all:

function loadIFrame() {
    window.frames['BodyFrame'].document.location.href = "iframe.html";
}         
function delayedLoad() {
    window.setTimeout(loadIFrame2, 5000);
}

Using:

<iframe src="" onLoad="delayedLoad()"></iframe>
like image 40
djangofan Avatar answered Oct 18 '22 00:10

djangofan


Load iFrame after page has loaded With JQuery and a little html and js

// Javascript
$(window).bind("load", function() {
  $('#dna_video').prepend('<iframe src="//player.vimeo.com/video/86554151" width="680" height="383" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>');
});
<!-- Append JQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- HTML -->
<div id="dna_video"></div>
like image 33
Amir Hosseinzadeh Avatar answered Oct 18 '22 00:10

Amir Hosseinzadeh


You can use defer attribute when need load last after css, js etc:

iframe defer src="//player.vimeo.com/video/89455262"
like image 41
user956584 Avatar answered Oct 18 '22 02:10

user956584