1

I am creating a new window in javascript and writing its contents using document.write():

var windowContent = '<!DOCTYPE html>';
windowContent += '<html>'
windowContent += '<head><title></title></head>';
windowContent += '<body>'
windowContent += '<img src="' + dataUrl + '" width="670px" style=">';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('','newWindow','width=100,height=100');
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
printWin.print();
setTimeout(function() {
  printWin.close();
}, 100);

In most browsers this works fine, but in Safari 6.0.2, the line printWin.document.write(windowContent); will occasionally crash the browser, and other times do nothing, resulting in a blank window.

Is there any alternative to document.write(); that I should be using instead? From googling, the general advice seems to be to use document.body.appendChild();, but the document doesn't have any content to begin with to append to.

4

0 回答 0