13

Here's the problem, which only occurs in Internet Explorer (IE). I have a page that has links to several different types of files. Links from these files execute a Javascript function that opens a new window and loads the specific file. This works great, unless the file I need to open in the new window is a PDF in which case the window is blank, even though the URL is in the address field. Refreshing that window using F5 doesn't help. However, if I put the cursor in the address field and press <enter> the PDF loads right up.

This problem only occurs in IE. I have seen it in IE 7 and 8 and am using Adobe Acrobat Reader 9. In Firefox (PC and Mac) everything works perfectly. In Chrome (Mac), the PDF is downloaded. In Safari (Mac) it works. In Opera (Mac) it prompts me to open or save. Basically, everything probably works fine, except for IE.

I have searched for similar problems and have seen some posts where it was suggested to adjust some of the Internet Options on IE. I have tried this but it doesn't help, and the problem wasn't exactly the same anyway.

Here's the Javascript function I use to open the new window.

function newwin(url,w,h) {
   win = window.open(url,"temp","width="+w+",height="+h+",menubar=yes,toolbar=yes,location=yes,status=yes,scrollbars=auto,resizable=yes");
   win.focus();
}

You can see that I pass in the URL as well as the height, h, and width, w, of the window. I've used a function like this for years and as far as I know have never had a problem.

I call the newwin() function using this.

<a href="javascript:newwin('/path/document.pdf',400,300)">document.pdf</a>

(Yes, I know there are other, better ways than using inline JS, and I've even tried some of them because I've run out of things to try, but nothing works.)

So, if anyone has an idea as to what might be causing this problem, I'd love to hear it.

Dean
  • 131
  • 1
  • 1
  • 3

2 Answers2

12

try:

function newwin(url,w,h) {
     var win = window.open("","temp","width="+w+",height="+h+",menubar=yes,toolbar=yes,location=yes,status=yes,scrollbars=auto,resizable=yes");
     win.location.href = url;
     win.focus();
}
David Murdoch
  • 87,823
  • 39
  • 148
  • 191
  • 2
    I had a similar problem, after applying your solution, the pdf now renders perfectly fine. Could you please explain how this works? Is it the zone or some security? Why doesn't it work when i use only window.open ? – SoftwareGeek May 11 '10 at 13:56
  • 1
    I'm pretty sure it is just a bug in IE's implementation (as usual). If it was a security issue then it wouldn't work at all. – David Murdoch May 11 '10 at 15:31
2

I have solved this problem with an invisible iframe

<div id="iframe" style="display: none">
  <iframe id="frm" style="display: none"></iframe>
</div>

And this logic here (using jquery):

if ($.browser.msie && ($.browser.version &lt; 9.0)) {
  frm.location.href = href;
}
else {
  window.open(href);
}

The behaviour in my case is exactly as if the document was opened in a popup, as I'm using

Content-Disposition: attachment; filename="document.pdf"
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509