Note that there are some explanatory texts on larger screens.

plurals
  1. POForm with target="_blank" behavior changed in IE 9 and 10
    primarykey
    data
    text
    <p>I have an app that uses something like the following code to open a dynamically generated report in a new tab. (I've mocked out the parameters for the sake of a demo.)</p> <pre><code>function gblPDFWdw(pdf) { var formDiv = document.createElement("div"); formDiv.innerHTML = "&lt;form method=post action='" + pdf + "' target='_blank'&gt;&lt;input type=hidden id=test name=test value='test'&gt;&lt;/form&gt;"; var form = formDiv.firstChild; document.body.appendChild(form); form.submit(); form.parentNode.removeChild(form); } </code></pre> <p>The <code>pdf</code> parameter us just the URL to the script that generates the PDF. The reason for the <code>form</code> manipulation is to get the parameters <code>POST</code>ed to the script rather than as <code>GET</code> parameters.</p> <p>In IE 8 and below, Chrome, Firefox, etc., this will do one of two things:</p> <ol> <li>If the browser can read PDFs (via built-in functionality or a plugin), it will open a new tab with the report, which is the desired behavior.</li> <li>If the browser can't read PDFs, it will open the new tab, immediately close it, and download the file.</li> </ol> <p>It seems that #2 has changed in IE 9 and above. It will open a new tab, but it yields only a blank screen until the user goes <strong>back to the original tab</strong>, clicks "Open" or "Save", and then the PDF downloads as expected. It's a very confusing interface.</p> <p>I suppose one way to work around this would be to generate a temporary PDF file, and then simply window.open() to it. Are there other ways to tweak the behavior of this functionality? Or is there a recommended practice that I've not run across?</p> <p>(Note: This isn't at all PDF specific; any file that the browser may download instead of display natively seems to be problematic.)</p> <p><strong>EDIT</strong>: Looks like I'm further behind the curve than I realized. It appears that this also occurs in IE9, and I'd never noticed as I never tried it without the PDF plugin installed or with another file type.</p> <p>Interestingly, the above code works like I'd expect if I <strong>remove</strong> the <code>input</code> field from the form. I'm not sure why the browser would treat that any differently. Of course, in my case, I need the input to <code>POST</code> data to my script.</p> <p><strong>EDIT 2</strong>: Silly mistake. <code>input</code> is self-closing. Fixed.</p> <p><strong>EDIT 3</strong>: Here are some screenshots to better explain the issue. I'm using <a href="http://jsbin.com/oxidan/3" rel="nofollow noreferrer">this JSBin</a> for testing. I have an onclick event to trigger the Javascript function above.</p> <p>The process starts on the first tab.</p> <p><img src="https://i.stack.imgur.com/dDU4y.png" alt="First tab, before a click."></p> <p>Then, once you click the text, a new tab opens. But it's blank!</p> <p><img src="https://i.stack.imgur.com/NORkW.png" alt="Second tab, opened via click."></p> <p>And it turns out that the <strong>original</strong> (now hidden) tab has a message asking whether to open or save the file.</p> <p><img src="https://i.stack.imgur.com/ISVfi.png" alt="The alert is on the first tab!"></p> <p>Clicking "Open" or "Save" will open the file just fine. But the process is terribly confusing, and inconsistent with other browsers.</p> <p><strong>EDIT 3</strong>: One step forward, one back. If I use <code>window.open()</code> to create a new window, and then programmatically set the <code>form.target</code> to the name of that window, I can at least get the Open/Save message in the opened tab…though I'd much prefer it to immediately close the tab like other browsers. Worse, with that technique, the new tab no longer immediately closes. Perhaps there's another technique?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload