javascript – How to display loading message when an iFrame is loading?-ThrowExceptions

Exception or error:

I have an iframe that loads a third party website which is extremely slow to load.

Is there a way I can display a loading message while that iframe loads the user doesn’t see a large blank space?

PS. Note that the iframe is for a third party website so I can’t modify / inject anything on their page.

How to solve:

I have done the following css approach:

<div class="holds-the-iframe"><iframe here></iframe></div>

.holds-the-iframe {
  background:url(../images/loader.gif) center center no-repeat;


I think that this code is going to help:


$('#foo').ready(function () {
    $('#loadingMessage').css('display', 'none');
$('#foo').load(function () {
    $('#loadingMessage').css('display', 'none');


<iframe src="" id="foo"></iframe>
<div id="loadingMessage">Loading...</div>


#loadingMessage {
    width: 100%;
    height: 100%;
    z-index: 1000;
    background: #ccc;
    top: 0px;
    left: 0px;
    position: absolute;


If it’s only a placeholder you are trying to achieve: a crazy approach is to inject text as an svg-background. It allows for some flexbility, and from what I’ve read the browser support should be fairly decent (haven’t tested it though):

  • Chrome >= 27
  • FireFox >= 30
  • Internet Explorer >= 9
  • Safari >= 5.1


<iframe class="iframe-placeholder" src=""></iframe>


   background: url('data:image/svg+xml;charset=utf-8,<svg xmlns="" width="100%" height="100%" viewBox="0 0 100% 100%"><text fill="%23FF0000" x="50%" y="50%" font-family="\'Lucida Grande\', sans-serif" font-size="24" text-anchor="middle">PLACEHOLDER</text></svg>') 0px 0px no-repeat;

What can you change?

Inside the background-value:

  • font size: look for font-size=”” and change the value to anything you want

  • font color: look for fill=””. Don’t forget to replace the # with %23 if you’re using hexadecimal color notation. %23 stands for a # in URL encoding which is necessary for the svg-string to be able to be parsed in FireFox.

  • font family: look for font-family=”” remember to escape the single quotes if you have a font that consists of multiple words (like with \’Lucida Grande\’)

  • text: look for the element value of the text-element where you see the PLACEHOLDER string. You can replace the PLACEHOLDER string with anything that is url-compliant (special characters need to be converted to percent notation)

Example on fiddle


  • No extra HTML-elements
  • No js
  • Text can easily (…) be adjusted without the need of an external program
  • It’s SVG, so you can easily put any SVG you want in there.


  • Browser support
  • It’s complex
  • It’s hacky
  • If the iframe-src doesn’t have a background set, the placeholder will shine through (which is not inherent to this method, but just standard behaviour when you use a bg on the iframe)

I would only recommend this only if it’s absolutely necessary to show text as a placeholder in an iframe which requires a little bit of flexbility (multiple languages, …). Just take a moment and reflect on it: is all this really necessary? If I had a choice, I’d go for @Christina’s method


    alert("iframe is done loading")

<iframe src="" style="display:none;" width="600" height="300"/>
<div class="loading" style="width:600px;height:300px;">iframe loading</div>


Here’s a quick solution for most of the cases:


.iframe-loading { 
    background:url(/img/loading.gif) center center no-repeat; 

You can use an animated loading GIF if you want to,


<div class="iframe-loading">
    <iframe src="http://your_iframe_url_goes_here"  onload="$('.iframe-loading').css('background-image', 'none');"></iframe>

Using the onload event you can remove the loading image after the source page is loaded inside your iframe.

If you are not using jQuery, just put an id into the div and replace this part of code:

$('.iframe-loading').css('background-image', 'none');

by something like this:

document.getElementById("myDivName").style.backgroundImage = "none";

All the best!


Yes, you could use a transparent div positioned over the iframe area, with a loader gif as only background.

Then you can attach an onload event to the iframe:

 $(document).ready(function() {

   $("iframe#id").load(function() {


<!DOCTYPE html>
<title>jQuery Demo - IFRAME Loader</title>
#frameWrap {
    height: 360px;
    width: 640px;
    border: 1px solid #777777;
    box-shadow:0px 0px 10px #777777;

#iframe1 {
    height: 360px;
    width: 640px;

#loader1 {
    border:1px solid #777777;
    box-shadow:0px 0px 10px #777777;

<script type="text/javascript" src=""></script>

<div id="frameWrap">
<img id="loader1" src="loading.gif" width="36" height="36" alt="loading gif"/>
<iframe id="iframe1" src="" ></iframe>
    $(document).ready(function () {
        $('#iframe1').on('load', function () {



You can use as below

$('#showFrame').on("load", function () {


I have followed the following approach

First, add sibling div

$('<div class="loading"></div>').insertBefore("#Iframe");

and then when the iframe completed loading


Leave a Reply

Your email address will not be published. Required fields are marked *