【问题标题】:Chrome displays body before head scripts are finishedChrome 在 head 脚本完成之前显示正文
【发布时间】:2015-07-13 12:30:57
【问题描述】:

据我所知,浏览器假设在到达脚本时停止渲染 DOM,直到脚本完成。

但是,如果我写一个

window.open('Mypage.aspx?ID=0', '_top'); 

在打开新页面之前,我仍然可以在 head 标签处看到快速的“闪烁”。 我什至试图在 body 标签之前创建一个人为的延迟,但无济于事。 我怎样才能避免这种闪烁?

例子:

<html>
<head>
<title>aa</title>
<script type="text/javascript">window.open('MyPage.aspx?ID=0', '_top');

 var now = new Date().getTime();
    while(new Date().getTime() < now + 3000){ /* force delay */ } 

</script></head><body style="background-color:red">
initial page
</body>

【问题讨论】:

  • 您可以在您的体型上设置display:none,而不是任何更复杂的东西,然后在您选择的任何时候显示或淡入。
  • @ne1410s - 当文档的正文足够大时,不幸的是它不起作用 - 例如,如果我将正文标签内的文本更改为 4 MB 的文本长度 - 它仍然会闪烁 :( –

标签: javascript html dom tags rendering


【解决方案1】:

在 Chrome 中,您可以在脚本的第一行使用 window.stop();,这将停止 html 呈现。

虽然对我来说,使用 css 类来管理它以在适当的时候隐藏/显示它似乎更一致。

【讨论】:

  • 在 Chrome 中隐藏 css 类对我不起作用,但 window.stop() 可以。我只是将其修改为if (typeof window.stop=='function'){window.stop();}; 以防止不支持window.stop() 的IE 出现错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 2014-10-02
  • 2012-12-31
  • 1970-01-01
相关资源
最近更新 更多