Failed to print iframe content from parent page in IE 11

Below is the javascript code that worked fine for me in IE 8 in Windows XP.

<script type="text/javascript"> function printFrame(frameId) { var iframe = $('#'+frameId)[0]; iframe.contentWindow.focus(); iframe.contentWindow.print(); } </script 

The above function is called, then on the parent page the "Print Frame" button is clicked.

 <iframe id="testFrame" src="" name="testFrame"> </iframe> <input type="button" onclick="printFrame('testFrame')" value="Print Frame"/> 

I recently upgraded my machine to Windows 7 and IE 8 to IE 11.

Now the same function does not give the expected result of printing the contents of the iframe. I tested this in chrome v34, firefox 30. This seems to work in them except IE 11.

When researching, I found that when an iframe src is dynamically set, iframe.contentWindow does not return an excluded iframe window object.

I tried using window.print () inside jsp in an iframe. This seems to work if src does not change dynamically. But I need to change the iframe content for the dynamic job to match the selection in another drop-down list on the parent page.

For example, check the link below in IE 11.

 http://plungjan.name/SO/testprintiframe.html 

The first time the link prints the content on an iframe. Then press the button. This causes the parent window to be sent to the printer instead of the contents of the iframe.

Then I tried another method. I wrote the code below in the contents of the iframe jsp and tried to access it from the parent page. I was unable to access the method itself. "script code in iframe jsp"

 <script type="text/javascript"> function printFrame() { window.print(); } </script 

"script code in parent jsp"

Try 1:

 <script type="text/javascript"> function printMyFrame(frameId) { var iframe = $('#'+frameId)[0]; $(iframe).contents().printFrame(); } </script 

Try 2:

 <script type="text/javascript"> function printMyFrame(frameId) { setTimeout(function() { window.frames[frameId].contentWindow.printFrame(); }, 200); } </script> 

I searched and implemented almost all google search methods to access the iframe window instead of the parent window. But it was not possible to print the contents of the iframe using IE 11.

Please tell me how to print iframe content in IE 11.

+6
source share
5 answers

I had a similar problem recently and it turned out that this method is necessary

 var target = document.getElementById('print-container'); try { target.contentWindow.document.execCommand('print', false, null); } catch(e) { target.contentWindow.print(); } }); 
+27
source

It worked for me

it works in chrome and firefox

 var result = contentWindow.document.execCommand('print', false, null); if (!result) contentWindow.print(); 
+13
source

It seems like the iframe cannot be printed via javascript in IE11. I tried several methods and was unsuccessful. This link says the same thing.

0
source

The following code works for me in both IE 10 and IE 11

 var browserName = navigator.userAgent.toLowerCase(); if ( browserName.indexOf("msie") != -1) { window.document.getElementById('pdfFrame').print(); } else if(browserName.indexOf("trident") != -1) { //IE 11 window.document.getElementById('pdfFrame').contentWindow.document.execCommand('print', false, null); } 
0
source

This is basically a paraphrase of the above answers (Nayas Subramanian) in jsFiddle, but security does not allow working in stackoverflow 100% ...

 $('#print').click(function () { var contentWindow = document.getElementById("frameContent").contentWindow; var result = contentWindow.document.execCommand('print', false, null); if(!result) contentWindow.print(); }); 
 iframe { width:100%; height:100%; border: none; } #print { position:absolute; left:20px; top: 20px; font-size: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <iframe id="frameContent" name="frameContent" src="https://stackoverflow.com/questions/24673024/unable-to-print-iframe-content-from-parent-page-in-ie-11#"></iframe> <button id="print">Print</button> 
0
source

Source: https://habr.com/ru/post/972017/


All Articles