How do I fire an event when a iframe has finished loading in jQuery?


I have to load a PDF within a page.

Ideally I would like to have a loading animated gif which is replaced once the PDF has loaded.

This question is tagged with javascript jquery

~ Asked on 2008-08-27 12:52:29

13 Answers


I'm pretty certain that it cannot be done.

Pretty much anything else than PDF works, even Flash. (Tested on Safari, Firefox 3, IE 7)

Too bad.

~ Answered on 2008-08-28 17:38:18


Have you tried:

$("#iFrameId").on("load", function () {
    // do something once the iframe is loaded

~ Answered on 2008-08-27 12:55:50


This did it for me (not pdf, but another "onload resistant" content):

<iframe id="frameid" src="page.aspx"></iframe>
<script language="javascript">
    iframe = document.getElementById("frameid");


    function WaitForIFrame() {
        if (iframe.readyState != "complete") {
            setTimeout("WaitForIFrame();", 200);
        } else {

    function done() {
        //some code after iframe has been loaded

Hope this helps.

~ Answered on 2009-08-28 03:23:37


I am trying this and seems to be working for me:

Bigger pdf file:

~ Answered on 2012-07-24 15:27:28


$("#iFrameId").ready(function (){
    // do something once the iframe is loaded

have you tried .ready instead?

~ Answered on 2009-06-01 07:51:25


I tried an out of the box approach to this, I havent tested this for PDF content but it did work for normal HTML based content, heres how:

Step 1: Wrap your Iframe in a div wrapper

Step 2: Add a background image to your div wrapper:

  background-position:center center; /*Can place your loader where ever you like */

Step 3: in ur iframe tag add ALLOWTRANSPARENCY="false"

The idea is to show the loading animation in the wrapper div till the iframe loads after it has loaded the iframe would cover the loading animation.

Give it a try.

~ Answered on 2011-12-14 05:34:32


Using both jquery Load and Ready neither seemed to really match when the iframe was TRULY ready.

I ended up doing something like this

$('#iframe').ready(function () {
    $("#loader").fadeOut(2500, function (sender) {

Where #loader is an absolutely positioned div over top the iframe with a spinner gif.

~ Answered on 2011-05-26 16:42:43


@Alex aw that's a bummer. What if in your iframe you had an html document that looked like:

    <meta http-equiv="refresh" content="0;url=/pdfs/somepdf.pdf" />

Definitely a hack, but it might work for Firefox. Although I wonder if the load event would fire too soon in that case.

~ Answered on 2008-08-28 05:06:20


I had to show a loader while pdf in iFrame is loading so what i come up with:

    loader({href:'loader.gif', onComplete: function(){
            $('#pd').html('<iframe onLoad="loader.close();" src="pdf" width="720px" height="600px" >Please wait... your report is loading..</iframe>');

I'm showing a loader. Once I'm sure that customer can see my loader, i'm calling onCompllet loaders method that loads an iframe. Iframe has an "onLoad" event. Once PDF is loaded it triggers onloat event where i'm hiding the loader :)

The important part:

iFrame has "onLoad" event where you can do what you need (hide loaders etc.)

~ Answered on 2013-03-21 03:02:48


Here is what I do for any action and it works in Firefox, IE, Opera, and Safari.

<script type="text/javascript">
  function actionIframe(iframe)
    ... do what ever ...
  function doMethod()
    var iFrames = document.getElementsByTagName('iframe');

    // what ever action you want.
    function iAction()
      // Iterate through all iframes in the page.
      for (var i = 0, j = iFrames.length; i < j; i++)

    // Check if browser is Safari or Opera.
    if ($.browser.safari || $.browser.opera)
      // Start timer when loaded.
        setTimeout(iAction, 0);

      // Safari and Opera need something to force a load.
      for (var i = 0, j = iFrames.length; i < j; i++)
         var iSource = iFrames[i].src;
         iFrames[i].src = '';
         iFrames[i].src = iSource;
      // For other good browsers.

~ Answered on 2008-11-19 14:00:00


If you can expect the browser's open/save interface to pop up for the user once the download is complete, then you can run this when you start the download:

$( document ).blur( function () {
    // Your code here...

When the dialogue pops up on top of the page, the blur event will trigger.

~ Answered on 2015-05-23 07:31:03


The solution I have applied to this situation is to simply place an absolute loading image in the DOM, which will be covered by the iframe layer after the iframe is loaded.

The z-index of the iframe should be (loading's z-index + 1), or just higher.

For example:

.loading-image { position: absolute; z-index: 0; }
.iframe-element { position: relative; z-index: 1; }

Hope this helps if no javaScript solution did. I do think that CSS is best practice for these situations.

Best regards.

~ Answered on 2016-06-22 10:46:58


Since after the pdf file is loaded, the iframe document will have a new DOM element <embed/>, so we can do the check like this:

    window.onload = function () {

    //creating an iframe element
    var ifr = document.createElement('iframe');

    // making the iframe fill the viewport
    ifr.width  = '100%';
    ifr.height = window.innerHeight;

    // continuously checking to see if the pdf file has been loaded
     self.interval = setInterval(function () {
        if (ifr && ifr.contentDocument && ifr.contentDocument.readyState === 'complete' && ifr.contentDocument.embeds && ifr.contentDocument.embeds.length > 0) {

            //You can do print here: ifr.contentWindow.print();
    }, 100); 

    ifr.src = src;

~ Answered on 2016-04-07 07:03:07

Most Viewed Questions: