I'm trying to create a printing function in javascript, it creates an iframe, sets the source and waits for the dom to be ready to print the contents of the iframe, but it's not working.
This is my function:
app.addReportPrintButtons = function (parentElement) {
var parent = parentElement || "body";
$(parent).on("click", ".print-report", function (e) {
var url = $(this).attr('href');
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", url);
iframe.onload = function (e) {
var win = iframe.contentWindow || iframe;
win.focus();
win.print();
};
e.preventDefault();
});
};
Can I print a page in a dynamically created iframe? or do the iframe needs to be attached to the DOM?