Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE won't load PDF in a window created with window.open

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.

like image 254
Dean Avatar asked Apr 27 '10 19:04

Dean


2 Answers

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"
like image 108
Lukas Eder Avatar answered Sep 19 '22 06:09

Lukas Eder


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();
}
like image 32
David Murdoch Avatar answered Sep 22 '22 06:09

David Murdoch