超全模态模型 × Harness 升级,升级解锁 ArkClaw,最新支持 DeepSeek-V4 系列与 GLM-5.1
被迫使用jQuery 2.2.4版本时,用<iframe>加载PDF的代码可以正常触发加载完成提示:
<iframe>
<html> <head> <title>Check pdf</title> <script src="https://code.jquery.com/jquery-2.2.4.js" crossorigin="anonymous"></script> <script> $(document).ready(function () { $("#pdf-holder").load(function () { alert("Finish loading"); }); setTimeout(function (){ $("#pdf-holder").attr('src', 'file.pdf') }, 5000); }); </script> </head> <body> <iframe id="pdf-holder" width="60%" height="500px" type="application/pdf" > Your browser cannot display PDF files </iframe> </body> </html>
但将<iframe>替换为<object>后,PDF能正常加载,但load事件无法触发:
<object>
load
<html> <head> <title>Check pdf</title> <script src="https://code.jquery.com/jquery-2.2.4.js" crossorigin="anonymous"></script> <script> $(document).ready(function () { $("#pdf-holder").load(function () { alert("Finish loading"); }); setTimeout(function (){ $("#pdf-holder").attr('data', 'file.pdf') }, 5000); }); </script> </head> <body> <object id="pdf-holder" width="60%" height="500px" type="application/pdf" > Your browser cannot display PDF files </object> </body> </html>
由于<object>的onload事件兼容性差,针对2022年主流浏览器环境,可采用以下几种方案判断PDF是否加载完成:
onload
readystatechange
部分浏览器会为<object>触发readystatechange事件,通过readyState属性判断加载状态:
readyState
$(document).ready(function () { $("#pdf-holder").on('readystatechange', function () { // readyState为complete表示资源加载完成 if (this.readyState === 'complete') { alert("Finish loading"); } }); setTimeout(function (){ $("#pdf-holder").attr('data', 'file.pdf') }, 5000); });
针对Chrome、Edge等使用内置PDF阅读器的浏览器,可轮询检测<object>的内部文档状态:
$(document).ready(function () { setTimeout(function (){ $("#pdf-holder").attr('data', 'file.pdf'); // 每500ms检查一次PDF加载状态 const checkTimer = setInterval(function() { const objEl = document.getElementById('pdf-holder'); // 尝试获取PDF内部文档(不同浏览器实现可能有差异) const innerDoc = objEl.contentDocument || objEl.getSVGDocument(); if (innerDoc && innerDoc.readyState === 'complete') { clearInterval(checkTimer); alert("Finish loading"); } }, 500); }, 5000); });
先通过XMLHttpRequest确认PDF文件已下载完成,再赋值给<object>的data属性,同时配合状态检测,稳定性更高:
data
$(document).ready(function () { setTimeout(function (){ const xhr = new XMLHttpRequest(); xhr.open('GET', 'file.pdf', true); xhr.responseType = 'blob'; xhr.onload = function() { if (xhr.status === 200) { // 将PDF转为Blob URL,避免重复下载 const blobUrl = URL.createObjectURL(xhr.response); $("#pdf-holder").attr('data', blobUrl); // 检测object的加载状态 const checkTimer = setInterval(function() { const objEl = document.getElementById('pdf-holder'); if (objEl.readyState === 'complete') { clearInterval(checkTimer); alert("Finish loading"); // 释放Blob URL资源 URL.revokeObjectURL(blobUrl); } }, 500); } }; xhr.send(); }, 5000); });
内容的提问来源于stack exchange,提问作者Ivan
超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起
模型再升级,30秒超长叙事, 模态参考扩容
模型自由,工具不限,最新支持 Deepseek-V4 系列、GLM-5.3 系列
超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列
大模型19元起,Al应用9.9元畅享,新人首购爆款尽享优惠