document.write非常慢,除了在Firefox中

时间:2013-09-22 23:30:38

标签: javascript jquery html css performance

我需要打开一个新窗口并在其上写一些html / css。这是我用来完成的代码:

                myHTML = '...' //Around 400 bytes of html and styling
                var doc = window.open('', 'preview', 'width=1200,height=600,scrollbars=1');
                doc.document.write(myHTML);
                doc.focus();
                $(window).unload(function() {
                        doc.close();
                });

我遇到的问题是,即使Firefox打开新窗口并立即写入/解析html / css,向用户显示没有延迟,Chrome和Safari需要大约5秒钟来显示它。

为什么会这样?任何建议的解决方法?

1 个答案:

答案 0 :(得分:0)

感谢您的评论...我本来会做一个jsfiddle,但网站暂时失败了......现在又回来了,但我解决了这个问题。问题是HTML中的这一行:

<link rel="stylesheet" type="text/css" href="~/templates/preview.css" />

出于某种原因,Chrome / Safari需要一些额外的时间来解析它。我改成了:

<link rel="stylesheet" type="text/css" href="http://www.fulldomain.com/templates/preview.css" />

现在一切正常。

相关问题