Safari document.write()替代方案

时间:2013-02-28 13:16:32

标签: javascript

我在javascript中创建一个新窗口并使用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);

在大多数浏览器中,这样可以正常工作,但在Safari 6.0.2中,行printWin.document.write(windowContent);偶尔会使浏览器崩溃,而其他时候什么也不做,导致窗口空白。

我应该使用document.write();替代吗?从谷歌搜索,一般建议似乎是使用document.body.appendChild();,但该文档没有任何内容可以开始追加。

0 个答案:

没有答案
相关问题