How do I print an IFrame from javascript
javascriptYou can print the content of an IFrame using JavaScript by calling the print() method on the contentWindow object of the IFrame. Here’s an example:
HTML:
<iframe id="my-iframe" src="http://example.com"></iframe>
<button onclick="printIframe()">Print</button>
JavaScript:
function printIframe() {
const iframe = document.getElementById('my-iframe');
const contentWindow = iframe.contentWindow;
contentWindow.print();
}
In this example, we first define an IFrame with the id “my-iframe” and a button that calls the printIframe() function when clicked.
The printIframe() function uses the document.getElementById() method to get a reference to the IFrame element, and then accesses its contentWindow property to get a reference to the window object that contains the content of the IFrame. Finally, it calls the print() method on the contentWindow object to print the content.
Note that the print() method will only work if the content of the IFrame is from the same origin as the page that contains the IFrame. If the IFrame contains content from a different origin, the browser’s security policies will prevent the content from being printed.
Other Article on Tag javascript
- - Adding and removing content in jQuery
- - Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
- - Can I change the Dojo namespace to something other than dojo
- - Can I prevent user pasting Javascript into Design Mode IFrame
- - dynamically create html element in javascript
- - Find XY of an HTML element with JavaScript
- - How can I upload files asynchronously with jQuery
- - How can you display Typing Speed using Javascript or the jQuery library
- - How do you capture mouse events in firefox
- - How to auto-size an iFrame