views:

11529

answers:

7

I have to load a pdf within a page (don't ask...)

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

Thanks

+3  A: 

Have you tried:

$("#iFrameId").load(function (){
    // do something once the iframe is loaded
});
travis
A: 

Thanks Travis but:

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

Works in IE & Safari, but not in Firefox for some reason. Firefox never seems to trigger the load event. It is something about the contents of the iFrame being a PDF it seems. If I substitute it for a html page it works perfectly.

Alex Andronov
A: 

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

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

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

travis
+11  A: 

I'm pretty certain that it cannot be done.

Check out the demo page I wrote:

http://sykari.net/stuff/iframe/

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

Too bad.

Antti Sykäri
A: 

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

<script type="text/javascript">
  $(document).ready(function(){
    doMethod();
  });
  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++)
      {
        actionIframe(iFrames[i]);
      }
    }

    // Check if browser is Safari or Opera.
    if ($.browser.safari || $.browser.opera)
    {
      // Start timer when loaded.
      $('iframe').load(function()
      {
        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;
      }
    }
    else
    {
      // For other good browsers.
      $('iframe').load(function()
      {
        actionIframe(this);
      }
      );
    }
  }
</script>
+1  A: 
$("#iFrameId").ready(function (){
    // do something once the iframe is loaded
});

have you tried .ready instead?

keithics
A: 

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");

    WaitForIFrame();

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

    function done() {
        //some code after iframe has been loaded
    }
</script>

Hope this helps...

Regards,

Ron Kuper

doesn't work in firefox with html content
Kevin