Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing iFrame only after its source content has been completely loaded

I have a iFrame on my page thats display style is none. I have a javascript function to set the source and then set the display to block. The problem is that the iframe shows up before the content of it is loaded and thus I get a flickering effect. It goes white first and then displays the content. So I need to set the source, and when done loading all content of its source, only set its display style.

CSS & Javascript

.ShowMe{display:block;}

function(url)
{
document.getElementById('myIFrame').src = url;
document.getElementById('myIFrame').className = ShowMe;
}
like image 681
Dean Martin Avatar asked Jul 11 '13 10:07

Dean Martin


People also ask

How do you check if iframe is loaded or it has a content?

setTimeout(function(){ if (//something shows iframe is loaded or has content) { //my code } else { $('#myIframe'). attr("src",""); //stop loading content } },5000);

How do I check if an iframe is empty?

If you want to check if no page loaded inside iframe, and the iframe is not cross-domain you could check for the existence of the body tag inside of the iframe. If it exists, then something loaded. If the iframe is cross-domain, you will be blocked by the same-origin policy.

Do iframes slow down page load?

Definitely iframe affects the page load performance and also it is not recommended to use iframe for many page security issues perspective. SEO companies strongly discourage iframes.


1 Answers

It's simple as that:

<iframe onload="this.style.opacity=1;" style="opacity:0;border:none;
like image 77
user3550138 Avatar answered Oct 13 '22 09:10

user3550138