javascript - "JavaScript placed at the end of the document so the pages load faster"是吗?

标签 javascript twitter-bootstrap

<分区>

Possible Duplicate:
Javascript on the bottom of the page?

我在一些推特 Bootstrap 示例中看到了一条评论。它说

JavaScript placed at the end of the document so the pages load faster

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

最佳答案

There are a number of advantages

  • 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.

There are some limitations as well

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 文件尚未完成加载,则没有事件应用于 元素(因为每个人都使用不引人注目的方法,对吗?) 并且用户可能会开始点击但没有得到预期的结果 结果。

  • 如果您有适当的后备方案,例如一个链接元素,如果支持 JavaScript 并且正确的事件已经发生,它通过 AJAX 获取内容 已应用,否则它是指向另一个页面的普通链接,然后 问题还不错。不过还是有点烦人。

有用的文章 Here

关于javascript - "JavaScript placed at the end of the document so the pages load faster"是吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14547062/

相关文章:

javascript - 范围 slider 控制的 Canvas 需要更好的图片加载和预览图像(?)

javascript - 使用 Angularjs 根据 json 中的值检查复选框

c# - 内联表单无法正常工作

javascript - 从 Rails 中的条件 View 调用 Bootstrap 模式

javascript - 跳过 Bootstrap Carousel 中隐藏的幻灯片

html - Bootstrap img-responsive 不缩放到父级

javascript - 从 JSON 获取参数

javascript - 自动刷新 Controller 和 View Yii

javascript - 使用异步数据对 Canvas 元素进行动画处理

html - 验证错误时 ASP.NET MVC5 文本区域未以红色标出