document.getElementById('print-button').onclick = function() { var iframe = document.createElement('iframe'); iframe.src = 'http://example.com'; document.body.appendChild(iframe); };
Of course, if you intend to attach more events of the same type, use addEventListener() .
If jQuery is at your disposal ...
$("#print-button").click(function() { $("<iframe />", { src: "http://example.com" }).appendTo("body"); });
source share