我必须在页面中加载 PDF。
理想情况下,我想要一个加载动画 gif,一旦 PDF 加载,它就会被替换。
你有没有尝试过:
$("#iFrameId").on("load", function () {
// do something once the iframe is loaded
});
我很确定这是不可能的。
几乎除了 PDF 之外的任何东西都可以使用,甚至是 Flash。(在 Safari、Firefox 3、IE 7 上测试)
太糟糕了。
这为我做了(不是pdf,而是另一个“onload抵抗”内容):
<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>
希望这可以帮助。
我正在尝试这个,似乎对我有用:http: //jsfiddle.net/aamir/BXe8C/
更大的pdf文件:http: //jsfiddle.net/aamir/BXe8C/1/
$("#iFrameId").ready(function (){
// do something once the iframe is loaded
});
你试过 .ready 吗?
我尝试了一种开箱即用的方法,我没有针对 PDF 内容进行测试,但它确实适用于基于 HTML 的普通内容,方法如下:
第 1 步:将您的 iframe 包装在 div 包装器中
第 2 步:将背景图片添加到 div 包装器中:
.wrapperdiv{
background-image:url(img/loading.gif);
background-repeat:no-repeat;
background-position:center center; /*Can place your loader where ever you like */
}
第 3 步:在你的 iframe 标签中添加 ALLOWTRANSPARENCY="false"
这个想法是在包装器 div 中显示加载动画,直到 iframe 在加载后加载 iframe 将覆盖加载动画。
试试看。
当 iframe 真正准备好时,同时使用 jquery Load 和 Ready 似乎都没有真正匹配。
我最终做了这样的事情
$('#iframe').ready(function () {
$("#loader").fadeOut(2500, function (sender) {
$(sender).remove();
});
});
其中#loader 是一个绝对定位的 div,位于 iframe 之上,带有一个微调器 gif。
@Alex aw 这真是太糟糕了。如果iframe你有一个看起来像这样的 html 文档怎么办:
<html>
<head>
<meta http-equiv="refresh" content="0;url=/pdfs/somepdf.pdf" />
</head>
<body>
</body>
</html>
绝对是一个 hack,但它可能适用于 Firefox。尽管我想知道在这种情况下加载事件是否会过早触发。
我必须在 iFrame 中的 pdf 加载时显示一个加载器,所以我想出了:
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>');
}
});
我正在展示一个装载机。一旦我确定客户可以看到我的加载器,我就会调用加载 iframe 的 onCompllet 加载器方法。iframe 有一个“onLoad”事件。加载 PDF 后,它会触发我隐藏加载程序的 onloat 事件:)
重要的部分:
iFrame 具有“onLoad”事件,您可以在其中执行所需的操作(隐藏加载程序等)
这是我为任何操作所做的,它适用于 Firefox、IE、Opera 和 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>
如果您可以期望在下载完成后为用户弹出浏览器的打开/保存界面,那么您可以在开始下载时运行它:
$( document ).blur( function () {
// Your code here...
});
当页面顶部弹出对话框时,会触发 blur 事件。
由于 pdf 文件加载后,iframe 文档会有一个新的 DOM 元素<embed/>,所以我们可以这样检查:
window.onload = function () {
//creating an iframe element
var ifr = document.createElement('iframe');
document.body.appendChild(ifr);
// 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) {
clearInterval(self.interval);
console.log("loaded");
//You can do print here: ifr.contentWindow.print();
}
}, 100);
ifr.src = src;
}
我针对这种情况应用的解决方案是简单的在 DOM 中放置一个绝对加载图片,在 iframe 加载完成后会被 iframe 层覆盖。
iframe 的 z-index 应该是(加载的 z-index + 1),或者更高。
例如:
.loading-image { position: absolute; z-index: 0; }
.iframe-element { position: relative; z-index: 1; }
如果没有 JavaScript 解决方案,希望这会有所帮助。我确实认为 CSS 是这些情况的最佳实践。
最好的祝福。
function frameLoaded(element) {
alert('LOADED');
};
<iframe src="https://google.com" title="W3Schools Free Online Web Tutorials" onload="frameLoaded(this)"></iframe>