How do I add a loading indicator to my page while my iframe loads?

14,058

Solution 1

Is there any reason you can't listen to the onload event of the iframe itself? It should fire after the child content has loaded.

Something like this:

showLoader();
$('#mydiv').html('<iframe src="sourcelink.html" frameborder="0" width="760" height="2400" scrolling="no"></iframe>');
$('#mydiv iframe').load(function() { hideLoader(); }

Solution 2

So In my case doing the following did for me..

The HTML

<iframe id="ifrmReportViewer" src="" frameborder="0" style="overflow:hidden;width:100%; height: 1000px;"></iframe>

and I was loading the iFrame on button of a click, so here is the JS

 $(document).ready(function () {

  $('body').on('click','#btnLoadiFrame',function () {

  ShowLoader();

  $('#ifrmReportViewer').attr('src', url);

  $('#ifrmReportViewer').load(function () {

    HideLoader(); 

  });
  });
 });
Share:
14,058

Related videos on Youtube

keybored
Author by

keybored

What is this I don't even?

Updated on May 14, 2022

Comments

  • keybored
    keybored about 2 years

    I am currently creating a page where upon clicking a link an iframe is inserted into a div and it's contents loaded. I do this using the following jQuery call:

    $('#mydiv').html('<iframe src="sourcelink.html" frameborder="0" width="760" height="2400" scrolling="no"></iframe>');
    

    Sometimes the source content loads very slowly and, as a result, it looks like nothing is happening. I would like to have a simple loading animation while the content is loading while the iframe's content loads. When the iframe finishes loading it's content should pop in and the loading animation should go away.

    I've been considering a couple ways I could do this (e.g. having a separate loader div to simply swap the two in and out) but I'm not sure of what the 'best' approach to solving this problem is. Perhaps I shouldn't be using .html()? I'm open to suggestion if there is a more correct solution.

  • keybored
    keybored over 13 years
    Hey thanks! This was ultimately the approach I took with a slight change. $('#mydiv').html('<iframe src="sourcelink.html" onLoad="hideLoader()" frameborder="0" width="760" height="2400" scrolling="no"></iframe>'); Is it any more or less correct to approach the problem this way?
  • blueyed
    blueyed over 10 years
    This might cause issues like the following: stackoverflow.com/questions/6799534/… (High CPU usage because of the animated gif below the iframe).