【问题标题】:How to detect when pdf is loaded in iframe when browser requests partial content range requests?当浏览器请求部分内容范围请求时,如何检测何时在 iframe 中加载 pdf?
【发布时间】:2015-04-26 20:44:33
【问题描述】:

我想检测 pdf 何时加载,但是当浏览器在 Chrome 40 中请求带有部分内容范围请求的 pdf 时,jQuery .load() 事件永远不会触发。

我想在加载 pdf 后触发浏览器 print() 函数。如果 pdf 足够小,浏览器不会请求范围,但对于较大的 pdf,我如何检测 pdf 加载?

function download(src){

    var iframe;

    function check() {
        console.log('checking..');
        if(iframe.contentWindow && iframe.contentWindow.document && iframe.contentWindow.document.body && iframe.contentWindow.document.body.children && iframe.contentWindow.document.body.children.length > 0){
            console.log('embed element exists... how do I tell when it\'s loaded?');
        } else {
            timeout = setTimeout(check, 150);
        }
    }

    var timeout = setTimeout(check, 150);
    iframe = $('<iframe type="application/pdf" src="'+src+'"></iframe>').appendTo(document.body).load(function() {
        console.log('iframe is loaded. This never happens when there are range requests');
    }).get(0);
}
$(document).ready(function(){
    download('test.pdf');
});

一个例子可以在http://stringham.me/pdf.html看到

【问题讨论】:

    标签: javascript jquery html pdf iframe


    【解决方案1】:

    这听起来可能是个愚蠢的答案,但是您是否尝试过 onload html 属性?

    【讨论】:

    • onload html 的行为类似于 jQuery 的 .load() 事件处理程序。如果浏览器没有执行多个请求范围来下载 pdf,它们都会触发。
    猜你喜欢
    • 2010-10-19
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 2012-05-14
    • 2015-08-03
    • 1970-01-01
    相关资源
    最近更新 更多