PDF - open in new window with js, or not?


I know it is possible to make a link to a pdf file open in a new window using javascript. I’ve got this working (and validating in 1.0Strict) by changing the start of my header to look like this -

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

<html xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”><head>
<meta http-equiv=“content-type” content=“text/html; charset=iso-8859-1” />
<script type=“text/javascript” src=“script.js”></script>

and then inserting the link to the pdf like this -

<p>Here’s the account by <a onclick=“window.open(this.href); return false;” href=“pdf docs/account.pdf” title=“This link will open a pdf file in a new window” class=“pdfFile”>Fred Smith</a>

I know that using pdf’s on websites has drawbacks. (Long term, I’m looking at a html solution to replace the pdf’s). That said, which is the preferred behaviour for what should happen when someone clicks on a link to a pdf - to let a pdf open without creating a new window, or using the js option as above so that the pdf opens in a new window?

Ignoring people’s feelings about PDF in general, you’ll probably annoy some percentage of your visitors no matter what you do. :-/

If you don’t open a new window, some users will close the whole browser when they’re done reading the PDF, since they’ll click the close button of the window out of habit.

If you do open a new window, people who have set the PDF plugin to open in a separate application window will be annoyed at the unnecessary blank window you cause.

Ideally, you should try to find out how many of your visitors have their plugins set to open PDFs in a separate window or not, and choose your method from that.

Since this is often impractical, I think opening a new window will be the lesser of two evils. (It’s what we do at our office site.) Forcing some users to close a redundant window is preferable to making others close the browser by mistake.

I would opt for just opening the pdf in a download. That way the only thing that closes is Adobe Reader and your window is still available.

Thanks both.

I notice that in IE7, the latest Opera and FF, without using the js approach, the pdf opens within the browser window area and without removing the normal ‘back’ button in the browser. I can see that someone could still close the site by accident, but personally, I’m finding it easier opening and closing the pdf using the browser controls and without opening a new window. I realise this may not mean this is right though.

What’s the accessibility view on the two methods? Is one more acceptable than the other?

If you read the help in Adobe Acrobat, you’ll find that PDF was meant to be a print program. For that reason, I prefer opening a pdf and saving it to my hard drive rather than opening it on the Internet. Also, although you can open pdfs in a browser, they often don’t display just the way they would if you opened them in Acrobat.

If you have access to the server and the server-side source code, perhaps you could modify the download thingie so that when the user press a downloadable link a download dialog is displayed, instead of linking directly to the files. If possible, this would be ideal. The user then can decide to either open the file or save it to disk. Should the user opt to open the file, the application that is set to handle that sort of files on the operating system is launched and in its own window, not interfering with the browser in any way. pdf-files would launch Acrobat Reader and .doc-files would launch MS Word on most systems.

This functionality is achieved by changing a few simple http headers:

response.setHeader("Content-Disposition", "attachment;filename="+ file.getName());

But if that is not an option I’d go with opening a new window. However I think it is important to be consistent with opening/not opening new browser windows. Word-files, high-resolution images or any other downloadable fileformat should not be treated differently. Just my 0.02 SEK. :slight_smile:

// JavaScript Document
function MM_openBrWindow(theURL,winName,features) { //v2.0
Name it openwindow.js

Between head tags
<script type=“text/javascript” src=“openwindow.js”></script>

The link
<a href=“javascript:;” onclick=“MM_openBrWindow(‘file.pdf’,‘’,‘’)”>Your File</a>

Hope this helps. Validates to strict

Yes, but what happens when a visitor doesn’t have JavaScript enabled (or when JavaScript code is silently filtered out by the company firewall)?

True. Only giving an answer. The choice is up to Frisbee.

Well, at the moment I’m going with allowing the pdf’s to open in the browser and using text to advise the user that the document is a pdf, and that they can choose to right click and save it rather than open in there and then.

And, as I mentioned, long term I’m hoping to use html solutions instead (which will mean simplifying the documents themselves first, because their present layout and length would involve (at my level) a mountainous hurdle to replicate in code).

Not ideal, but going in the right direction I hope.