0
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="gistfile1.js"></script>
</head>

<body>
    <p id="divPDF">Can't see the PDF?</p>
</body>

<script type="text/javascript">
    var info = getAcrobatInfo();
    if (info.acrobat == false){
        window.location = "empty.pdf";
    } else {
        document.getElementById("divPDF").innerHTML = "<object data='empty.pdf' type='application/pdf' width='100%' height='100%'><p class='helpContent'>Can't see the PDF? Try <a href='empty.pdf' download='empty.pdf'>downloading the file</a>. If you're still having trouble, <a href='/contactUs.jsp'>contact us</a> or email us directly at <a href='mailto:support@guitarinstructor.com'>support@guitarinstructor.com</a>.</p></object>";
    }
</script>

</html>

Here's what I'm looking at doing. IF the getAcrobatInfo TRUE, display the PDF. If the getAcrobat returns FALSE, then automatically download the PDF.

I thought this would work on a window.location = "URL OF THE PDF", my browser still attempts to display it.

Is there any javascript or jQuery that I can implement that will allow me to force an auto download if the user's browser doesn't have a PDF Reader plugin?

Murphy1976
  • 1,415
  • 8
  • 40
  • 88
  • As far as I know the browser will always try to open what you navigate to if it can. You would need to change the header of the response that comes from the server to make the browser think that it can't handle it, but that can't be done using client script. Example: http://stackoverflow.com/questions/8590543/force-browser-to-download-pdf-document-instead-of-opening-it – Guffa Jul 08 '14 at 21:09

1 Answers1

0

Typically you must set the Content-disposition header to attachment to encourage a browser to download a file instead of displaying it. If you can do this, and your client side plugin detection works, you could edit your server side code to have a different URL depending on the disposition you want to send, and have that in your object src attribute or location.replace argument.

Additionally, in HTML5 there is a download attribute for the <a> tag, with an optional filename as the attribute value. Support for this is not in every browser.

<a href="path/to/file.pdf" download="filename.pdf">Download</a>

Note that a very common practice is to have a single link with a small blurb informing a user how to download a file (e.g. using the right mouse button).

shanet
  • 344
  • 2
  • 7
  • I figured as much, but what was asked of me was to see if the native plugin was not active, that the page would simply redirect to the PDF itself. Looking at this, it's doesn't make much sense to point to the file when the browser can't look at the file in the first place. – Murphy1976 Jul 09 '14 at 12:37