最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I detect if a browser will display or download a PDF using JavaScript? - Stack Overflow

programmeradmin3浏览0评论

Say I have:

  • a webpage with an iFrame: <iframe src="" style="display:none;"></iframe>
  • an URL pointing to a PDF document: .pdf
  • some JavaScript that will do iframe.src = pdfurl
  • a button that will trigger such JavaScript
  • if the browser is going to display the PDF inline, the button will say "View PDF" and when clicked will make the iFrame visible
  • otherwise it will say "Download PDF"

I found a way to detect whether the PDF has been loaded in the iFrame: reading iframe.contentDocument.contentType after onload has fired, but

  • this won't allow me to display the correct button
  • onload does not fire if the file is being downloaded

Thanks :)

Say I have:

  • a webpage with an iFrame: <iframe src="" style="display:none;"></iframe>
  • an URL pointing to a PDF document: http://www.example./file.pdf
  • some JavaScript that will do iframe.src = pdfurl
  • a button that will trigger such JavaScript
  • if the browser is going to display the PDF inline, the button will say "View PDF" and when clicked will make the iFrame visible
  • otherwise it will say "Download PDF"

I found a way to detect whether the PDF has been loaded in the iFrame: reading iframe.contentDocument.contentType after onload has fired, but

  • this won't allow me to display the correct button
  • onload does not fire if the file is being downloaded

Thanks :)

Share Improve this question edited May 1, 2024 at 10:38 matronator 5547 silver badges16 bronze badges asked May 18, 2009 at 12:01 Matteo CaprariMatteo Caprari 2,9794 gold badges27 silver badges36 bronze badges 2
  • 1 Users don't understand the difference between viewing a file in the browser and downloading it anyway. IMO "View PDF" is fine in both cases, because it will open in their PDF viewer or browser plugin. If they don't even have a viewer, the browser/shell will display a "system can't display PDF" message, making them understand they need some additional software, which you could supply with one of these nice "Get Adobe PDF" links (though I'd prefer Foxit Reader). So I would not really bother to get this right, because you never know which plugin exactly the user uses and whether it is enabled. – OregonGhost Commented May 18, 2009 at 12:07
  • 1 sounds like an answer, not a ment – geowa4 Commented May 18, 2009 at 12:24
Add a ment  | 

4 Answers 4

Reset to default 5

To tell the client's browser to download a response as a file, set the Content-Disposition HTTP header to 'attachment' in your response. This is no guarantee, but it's the proper method of telling the browser how to handle the content.

§ 8.9.1.6 PDF viewing support

window.navigator.pdfViewerEnabled

Returns true if the user agent supports inline viewing of PDF files when navigating to them, or false otherwise. In the latter case, PDF files will be handled by external software.

MDN web docs article.

Browser patibility table

In modern Browsers, JavaScripts global Navigator object has a plugins property, filled with an array of Plugins, if you can find a Plugin for Mimetype application/pdf, you can safely assume that the browser will display pdf files inline, as long as the server does not explicit send content-disposition: attachment headers, of course.

You could send a HEAD request to that resource and check what Content-Type and Content-Disposition values are being sent back. Based on these information you could determine whether a browser would display or download that resource.

发布评论

评论列表(0)

  1. 暂无评论