HTML 嵌入式 PDF 和加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1138232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML Embedded PDF's & onload
提问by
I'm embedding a PDF in a web page with the following html :-
我正在使用以下 html 将 PDF 嵌入网页中:-
<object id="pdf" classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="1024" height="600">
<param name="SRC" value="/GetDoc.ashx?SOID=<%=Html.Encode(Model.OrderID)%>" />
<embed src="/GetDoc.ashx?SOID=<%=Html.Encode(Model.OrderID)%>" width="1024" height="600">
<noembed> Your browser does not support embedded PDF files. </noembed>
</embed>
</object>
The PDF's can be a little slow to load so I'd like to hide the object and display a loading message / gif until it's fully loaded so the user isn't looking at a blank screen.
PDF 的加载速度可能有点慢,所以我想隐藏对象并显示加载消息/gif,直到它完全加载,这样用户就不会看到一个空白屏幕。
All I really need is a way of telling when the object is fully loaded. I've tried the 'onload' event of the but it never seems to get fired.
我真正需要的是一种告诉对象何时完全加载的方法。我已经尝试过 'onload' 事件,但它似乎从未被解雇。
I'm beginning to think it might not be possible, but it never hurts to ask...
我开始认为这可能是不可能的,但询问永远不会有什么坏处......
回答by DougB
I don't know why everyone makes it so hard.
我不知道为什么每个人都这么难。
<object data="yourfile.pdf" style="background: transparent url(AnimatedLoading.gif) no-repeat center;" type="application/pdf" />
回答by user124118
Following code works.
以下代码有效。
<div style="background: transparent url(loading.gif) no-repeat">
<object height="1250px" width="100%" type="application/pdf" data="aaa.pdf">
<param value="aaa.pdf" name="src"/>
<param value="transparent" name="wmode"/>
</object>
</div>
回答by Pavel Savara
I'm loading the PDF with jQuery ajax into browser cache. Then I create embedded element with data already in browser cache.
我正在将带有 jQuery ajax 的 PDF 加载到浏览器缓存中。然后我使用浏览器缓存中已有的数据创建嵌入元素。
var url = "http://example.com/my.pdf";
// show spinner
$.mobile.showPageLoadingMsg('b', note, false);
$.ajax({
url: url,
cache: true,
mimeType: 'application/pdf',
success: function () {
// display cached data
$(scroller).append('<embed type="application/pdf" src="' + url + '" />');
// hide spinner
$.mobile.hidePageLoadingMsg();
}
});
You have to set your http headers correctly as well.
您还必须正确设置 http 标头。
HttpContext.Response.Expires = 1;
HttpContext.Response.Cache.SetNoServerCaching();
HttpContext.Response.Cache.SetAllowResponseInBrowserHistory(false);
HttpContext.Response.CacheControl = "Private";
回答by Michael
None of the recommendations are valid, because DOM is loaded before the PDF content is loaded. So DOM can't control ActiveX content
没有任何建议是有效的,因为 DOM 在加载 PDF 内容之前加载。所以DOM无法控制ActiveX内容
回答by Amir Shahzad
$(document).ready(function() {
});
would not work as this fires mostly on document.readyState == interactive
rather than on document.readyState == complete
.
不会起作用,因为这主要是在document.readyState == interactive
而不是 on 上触发document.readyState == complete
。
If you put a timer with this check (document.readyState == "complete"
) it would definitely work!
如果您在此检查 ( document.readyState == "complete"
) 上放置一个计时器,它肯定会起作用!
回答by jmjm
<embed onload='alert("I should be called")'></embed>
回答by geowa4
"Content within a tag is displayed when an object is loading, but hasn't yet finished."
“当对象正在加载但尚未完成时,将显示标签中的内容。”
So put your spinner in there and it should work out nicely for you. And you won't have to write any JavaScript.
所以把你的微调器放在那里,它应该适合你。而且您不必编写任何 JavaScript。
回答by Matt Bridges
You are going to want something like jQuery's document.ready()
function. For non-IE browsers, you can register a handler for the event DOMContentLoaded
, and your handler function will be called after all the images and objects have been loaded. For IE, you have to continually check the document.readyState
property, and wait for it to be "complete"
.
你会想要类似 jQuery 的document.ready()
函数的东西。对于非 IE 浏览器,您可以为 event 注册一个处理程序DOMContentLoaded
,并且您的处理程序函数将在所有图像和对象加载完毕后被调用。对于 IE,您必须不断检查该document.readyState
属性,并等待它成为"complete"
.
If you are using jQuery, they've done the hard work for you, so all you have to do is:
如果您正在使用 jQuery,他们已经为您完成了艰苦的工作,所以您所要做的就是:
$(document).ready(function() {
//take away the "loading" message here
});
If you don't want to use jquery, you'd have to do something like:
如果您不想使用 jquery,则必须执行以下操作:
addEventListener('DOMContentLoaded', function() {
//take away the "loading" message here
});
function waitForIE() {
if (!document.all) return;
if (document.readyState == "complete") {
//take away the "loading" message here
}
else {
setTimeout(waitForIE, 10);
}
}
waitForIE();