“将JavaScript放置在文档的末尾,以便页面加载速度更快".真的? [英] "JavaScript placed at the end of the document so the pages load faster" TRUE?

查看:76
本文介绍了“将JavaScript放置在文档的末尾,以便页面加载速度更快".真的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
页面底部的Javascript?

Possible Duplicate:
Javascript on the bottom of the page?

在一些Twitter引导示例中,我看到了一条评论.它说

I saw a comment in some twitter bootstrap example. It says

JavaScript置于文档末尾,因此页面加载速度更快

这是真的吗?如果是,那么它是如何工作的??

Is this true?? If yes then How it works??

推荐答案

有很多优点

  • 无需检查DOM是否已加载,因为通过 最后的脚本,您肯定知道.
  • JavaScript脚本 必须先完全加载文件,然后才能启动网络浏览器 下一个JavaScript文件.这样做的效果,如果是JavaScript文件 包括在文档的顶部,这将是一个视觉 最终用户看到实际页面之前的延迟.这是完全 如果您在末尾添加了JavaScript文件,则可以避免 文档.
  • There’s no need to have a check if the DOM is loaded, since by having the scripts at the end, you know for sure it is.
  • A JavaScript script file has to be loaded completely before a web browser even begins on the next JavaScript file. The effect of this, if the JavaScript files are included at the top of the document, is that it will be a visual delay before the end user sees the actual page. This is completely avoided if you include the JavaScript files at the end of the document.

也有一些限制

尽管在底部包含JavaScript文件,但可以帮助我们解决 延迟页面渲染,从而给人留下印象的问题 网站上每个页面的加载速度都比较快,这确实有一个缺点.

While including the JavaScript files at the bottom helps us around the problem of delaying the page rendering, thus giving the impression that each page in the web site loads faster, it does have a drawback.

  • 如果该页面对最终用户可见,但是JavaScript文件尚未完成加载,则没有事件应用于 元素还没有(因为每个人都使用一种不引人注目的方法,对吗?) 并且用户可能开始四处点击而无法获得预期的效果 结果.

    • If the page is visible to the end user, but the JavaScript files haven’t finished loading yet, no events have been applied to the elements yet (because everyone uses an unobtrusive approach, right?) and the user might start clicking around and not getting the expected results.

      如果您有适当的后备广告,例如一个link元素,如果支持JavaScript且已发生适当的事件,则该元素通过AJAX获取内容 应用,否则它是指向另一个页面的普通链接,然后 问题不是那么严重.不过还是有些烦人.

      If you have proper fallbacks, e.g. a link element which gets content via AJAX if JavaScript is supported and the proper event has been applied, otherwise it’s a normal link leading to another page, then the problem isn’t that bad. Still somewhat annoying, though.

      有用的文章此处

      这篇关于“将JavaScript放置在文档的末尾,以便页面加载速度更快".真的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆