var iframe = document.createElement('iframe');
iframe.onload = function() {
var doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
doc.getElementsByTagName('body')[0].innerHTML = "<p>1234</p>";
iframe.contentWindow.focus(); // This is key, the iframe must have focus first
iframe.contentWindow.print();
}
document.getElementsByTagName('body')[0].appendChild(iframe);