<p>I have to load a PDF within a page.</p>

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

[{'answer_id': 30012, 'author': 'travis', 'author_id': 1414, 'author_profile': 'https://Stackoverflow.com/users/1414', 'pm_score': 8, 'selected': False, 'text': '<p>Have you tried:</p>\n\n<pre><code>$("#iFrameId").on("load", function () {\n    // do something once the iframe is loaded\n});\n</code></pre>\n'}, {'answer_id': 31692, 'author': 'travis', 'author_id': 1414, 'author_profile': 'https://Stackoverflow.com/users/1414', 'pm_score': 2, 'selected': False, 'text': '<p>@Alex aw that\'s a bummer. What if in your <code>iframe</code> you had an html document that looked like:</p>\n\n<pre><code>&lt;html&gt;\n  &lt;head&gt;\n    &lt;meta http-equiv="refresh" content="0;url=/pdfs/somepdf.pdf" /&gt;\n  &lt;/head&gt;\n  &lt;body&gt;\n  &lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>Definitely a hack, but it might work for Firefox. Although I wonder if the load event would fire too soon in that case.</p>\n'}, {'answer_id': 32878, 'author': 'Antti Kissaniemi', 'author_id': 2948, 'author_profile': 'https://Stackoverflow.com/users/2948', 'pm_score': 6, 'selected': True, 'text': "<p>I'm pretty certain that it cannot be done.</p>\n\n<p>Pretty much anything else than PDF works, even Flash. (Tested on Safari, Firefox 3, IE 7)</p>\n\n<p>Too bad.</p>\n"}, {'answer_id': 301951, 'author': 'user22367', 'author_id': 22367, 'author_profile': 'https://Stackoverflow.com/users/22367', 'pm_score': 0, 'selected': False, 'text': '<p>Here is what I do for any action and it works in Firefox, IE, Opera, and Safari.</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n  $(document).ready(function(){\n    doMethod();\n  });\n  function actionIframe(iframe)\n  {\n    ... do what ever ...\n  }\n  function doMethod()\n  {   \n    var iFrames = document.getElementsByTagName(\'iframe\');\n\n    // what ever action you want.\n    function iAction()\n    {\n      // Iterate through all iframes in the page.\n      for (var i = 0, j = iFrames.length; i &lt; j; i++)\n      {\n        actionIframe(iFrames[i]);\n      }\n    }\n\n    // Check if browser is Safari or Opera.\n    if ($.browser.safari || $.browser.opera)\n    {\n      // Start timer when loaded.\n      $(\'iframe\').load(function()\n      {\n        setTimeout(iAction, 0);\n      }\n      );\n\n      // Safari and Opera need something to force a load.\n      for (var i = 0, j = iFrames.length; i &lt; j; i++)\n      {\n         var iSource = iFrames[i].src;\n         iFrames[i].src = \'\';\n         iFrames[i].src = iSource;\n      }\n    }\n    else\n    {\n      // For other good browsers.\n      $(\'iframe\').load(function()\n      {\n        actionIframe(this);\n      }\n      );\n    }\n  }\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 933899, 'author': 'keithics', 'author_id': 115330, 'author_profile': 'https://Stackoverflow.com/users/115330', 'pm_score': 3, 'selected': False, 'text': '<pre><code>$("#iFrameId").ready(function (){\n    // do something once the iframe is loaded\n});\n</code></pre>\n\n<p>have you tried .ready instead?</p>\n'}, {'answer_id': 1344874, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>This did it for me (not pdf, but another "<code>onload</code> resistant" content): </p>\n\n<pre><code>&lt;iframe id="frameid" src="page.aspx"&gt;&lt;/iframe&gt;\n&lt;script language="javascript"&gt;\n    iframe = document.getElementById("frameid");\n\n    WaitForIFrame();\n\n    function WaitForIFrame() {\n        if (iframe.readyState != "complete") {\n            setTimeout("WaitForIFrame();", 200);\n        } else {\n            done();\n        }\n    }\n\n    function done() {\n        //some code after iframe has been loaded\n    }\n&lt;/script&gt;  \n</code></pre>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 6142082, 'author': 'Chris Marisic', 'author_id': 37055, 'author_profile': 'https://Stackoverflow.com/users/37055', 'pm_score': 2, 'selected': False, 'text': '<p>Using both jquery Load and Ready neither seemed to really match when the iframe was TRULY ready.</p>\n\n<p>I ended up doing something like this</p>\n\n<pre><code>$(\'#iframe\').ready(function () {\n    $("#loader").fadeOut(2500, function (sender) {\n        $(sender).remove();\n    });\n});\n</code></pre>\n\n<p>Where #loader is an absolutely positioned div over top the iframe with a spinner gif.</p>\n'}, {'answer_id': 8499902, 'author': 'Michael Lu', 'author_id': 778964, 'author_profile': 'https://Stackoverflow.com/users/778964', 'pm_score': 3, 'selected': False, 'text': '<p>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:</p>\n\n<p><strong>Step 1</strong>: Wrap your Iframe in a div wrapper    </p>\n\n<p><strong>Step 2</strong>: Add a background image to your div wrapper:</p>\n\n<pre><code>.wrapperdiv{\n  background-image:url(img/loading.gif);\n  background-repeat:no-repeat;\n  background-position:center center; /*Can place your loader where ever you like */\n}\n</code></pre>\n\n<p><strong>Step 3</strong>: in ur iframe tag add  <code>ALLOWTRANSPARENCY="false"</code> </p>\n\n<p>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. </p>\n\n<p>Give it a try.</p>\n'}, {'answer_id': 11634193, 'author': 'Aamir Afridi', 'author_id': 569751, 'author_profile': 'https://Stackoverflow.com/users/569751', 'pm_score': 3, 'selected': False, 'text': '<p>I am trying this and seems to be working for me:\n<a href="http://jsfiddle.net/aamir/BXe8C/" rel="noreferrer">http://jsfiddle.net/aamir/BXe8C/</a></p>\n\n<p>Bigger pdf file:\n<a href="http://jsfiddle.net/aamir/BXe8C/1/" rel="noreferrer">http://jsfiddle.net/aamir/BXe8C/1/</a></p>\n'}, {'answer_id': 15538712, 'author': 'rinchik', 'author_id': 2387506, 'author_profile': 'https://Stackoverflow.com/users/2387506', 'pm_score': 1, 'selected': False, 'text': '<p>I had to show a loader while pdf in iFrame is loading so what i come up with:</p>\n\n<pre><code>    loader({href:\'loader.gif\', onComplete: function(){\n            $(\'#pd\').html(\'&lt;iframe onLoad="loader.close();" src="pdf" width="720px" height="600px" &gt;Please wait... your report is loading..&lt;/iframe&gt;\');\n    }\n    });\n</code></pre>\n\n<p>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 :)</p>\n\n<p><strong>The important part:</strong></p>\n\n<p>iFrame has "onLoad" event where you can do what you need (hide loaders etc.)</p>\n'}, {'answer_id': 30410165, 'author': 'Ethan', 'author_id': 2932891, 'author_profile': 'https://Stackoverflow.com/users/2932891', 'pm_score': 0, 'selected': False, 'text': "<p>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:</p>\n\n<pre><code>$( document ).blur( function () {\n    // Your code here...\n});\n</code></pre>\n\n<p>When the dialogue pops up on top of the page, the blur event will trigger.</p>\n"}, {'answer_id': 36468935, 'author': '令狐葱', 'author_id': 911838, 'author_profile': 'https://Stackoverflow.com/users/911838', 'pm_score': 0, 'selected': False, 'text': '<p>Since after the pdf file is loaded, the iframe document will have a new DOM element <code>&lt;embed/&gt;</code>, so we can do the check like this:</p>\n\n<pre><code>    window.onload = function () {\n\n\n    //creating an iframe element\n    var ifr = document.createElement(\'iframe\');\n    document.body.appendChild(ifr);\n\n    // making the iframe fill the viewport\n    ifr.width  = \'100%\';\n    ifr.height = window.innerHeight;\n\n    // continuously checking to see if the pdf file has been loaded\n     self.interval = setInterval(function () {\n        if (ifr &amp;&amp; ifr.contentDocument &amp;&amp; ifr.contentDocument.readyState === \'complete\' &amp;&amp; ifr.contentDocument.embeds &amp;&amp; ifr.contentDocument.embeds.length &gt; 0) {\n            clearInterval(self.interval);\n\n            console.log("loaded");\n            //You can do print here: ifr.contentWindow.print();\n        }\n    }, 100); \n\n    ifr.src = src;\n}\n</code></pre>\n'}, {'answer_id': 37965768, 'author': 'AldorEla', 'author_id': 3454908, 'author_profile': 'https://Stackoverflow.com/users/3454908', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n\n<p>The z-index of the iframe should be (loading's z-index + 1), or just higher.</p>\n\n<p>For example:</p>\n\n<pre><code>.loading-image { position: absolute; z-index: 0; }\n.iframe-element { position: relative; z-index: 1; }\n</code></pre>\n\n<p>Hope this helps if no javaScript solution did. I do think that CSS is best practice for these situations.</p>\n\n<p>Best regards.</p>\n"}, {'answer_id': 68452436, 'author': 'Mahdi Bashirpour', 'author_id': 6569224, 'author_profile': 'https://Stackoverflow.com/users/6569224', 'pm_score': 1, 'selected': False, 'text': '<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>function frameLoaded(element) {\n    alert(\'LOADED\');\n};</code></pre>\r\n<pre class="snippet-code-html lang-html prettyprint-override"><code> &lt;iframe src="https://google.com" title="W3Schools Free Online Web Tutorials" onload="frameLoaded(this)"&gt;&lt;/iframe&gt; </code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}]
['https://Stackoverflow.com/questions/30005', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3168/']