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!
11 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

- 8,798
- 5
- 49
- 73
-
It turns out your first line solved everything. I was using a horrible template for tabs that I downloaded from a random website, and it liked to render my hidden iframe in front of everything unti it fully loaded. When I switched over to using jquery tabs (which looked much nicer to boot!), the iframe rendering was no longer an issue. jQuery ROCKS! Thanks :) – Sean Oct 20 '09 at 18:03
-
In IE, this may break the back button because it adds the new iframe source to the browser history. – Sjoerd Apr 08 '11 at 10:31
-
13The number of people answering in jQuery when asked for Javascript is scary these days. – Piyush Soni Feb 14 '14 at 23:33
-
2@PiyushSoni We spend years getting people acclimated to using a comprehensive solution like jQuery to assist with browser consistency and now purists are complaining about it? Educating people works better than complaining. :) – James Lee Baker Apr 08 '16 at 22:25
You can use the loading attribute to lazy load iframes.
<iframe src="https://example.com"
loading="lazy"
width="600"
height="400"></iframe>

- 768
- 15
- 22
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; }

- 3
- 1
- 3

- 106,652
- 57
- 273
- 297
-
Hey Eskimo- I assume your answer would work, although I wasn't able to make it work (I'm sure it was due to a problem on my end) :) I don't have enough reputation to vote you up yet, but thanks for the suggestion! – Sean Oct 20 '09 at 18:07
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.

- 1,126
- 8
- 12
Use Javascript in the onLoad event, or in the button click handler, to set the src attribute of the iframe.

- 36,858
- 7
- 80
- 143
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>

- 28,471
- 61
- 196
- 289
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>

- 7,360
- 4
- 18
- 33
-
1This code is the only one that allowed me to load an iFrame after canvas.js. With this code, the canvas instantly appears and the iFrame is delayed, whereas before, the canvas wouldn't appear until after the iFrame loaded. Thanks Amir! – Joe Oct 14 '21 at 03:30
You can use defer attribute when need load last after css, js etc:
iframe defer src="//player.vimeo.com/video/89455262"

- 5,316
- 3
- 40
- 50
window.setTimeout(function(){
$("#myframe").attr("src","exp3.html");
$("#myframediv").show("slow");
},4000);
try this, this also works.

- 2,533
- 4
- 29
- 51

- 191
- 1
- 5
You can also apply display:none to the iframe with CSS, then use jQuery's .show like this:
$(document).ready(function(){
$('iframe.class_goes_here').show();
});

- 23
- 1
-
8The iframe will still load even if in a `display:none` dom subtree - it just won't show. – Eamon Nerbonne Aug 28 '13 at 09:35
Or my favorite use
setTimeout('your app', 6000)
That will make it wait x number of milliseconds to call any function....

- 25,481
- 10
- 85
- 128

- 1,869
- 5
- 34
- 57