在底部加载JS的好处,而不是文档的顶部 [英] Benefits of loading JS at the bottom as opposed to the top of the document

查看:58
本文介绍了在底部加载JS的好处,而不是文档的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档底部加载JS的真正好处是什么(如果有的话),而不是顶部。看起来页面加载和JS依赖功能有一个短暂的延迟。

我开始使用 html5boilerplate 开始我的所有模板,但我不确定有利于加载JS的底部是。



它是否真的有所作为,如果是这样,为什么?
<如果您在页面底部包含外部js文件,则可以将HTTP请求的优先级赋予视觉效果显示将呈现给客户,而不是交互或动态的逻辑。我相信,如果您不使用内容交付网络将图像传送到客户端,那么您一次最多只能处理2个HTTP请求。你不想浪费这些请求的逻辑,因为我们都知道最终用户是多么不耐烦。通过在文件结尾处加载js,您可以访问DOM(大部分时间),而无需调用document.ready()函数。你知道,如果页面渲染最终会将它传递给你的JavaScript代码,那么必要的页面元素通常已经加载了。

还有其他几个原因,但是当我感觉如果把所有j放在底部时感到非常尴尬时,我会记住这些重要的原因。


What are the real benefits (if any) to loading your JS at the bottom of the document, as opposed to the top. It seems there is a brief delay in page loading and JS dependent functionality.

I am using html5boilerplate for beginning all my templates, but am not really sure on how beneficial loading the JS at the bottom is.

Does it really make all that much of a difference, and if so, why?

解决方案

  1. If you include external js files at the bottom of your page, you give the priority of your HTTP requests to the visual display that will be presented to the client instead of to the logic of interaction or dynamics. I believe, if you do not use a content delivery network to deliver images to the client then you are only allowed to process a maximum of 2 HTTP requests at a time. You do not want to waste these requests on logic because we all know how impatient the end user is.

  2. By loading js at then end of the file you can access the DOM(most of the time) without having to call a document.ready() function. You know that if the page render finally makes it to your javascript code that the necessary page elements have usually loaded already.

There are a few more reasons out there but these are the important ones that I try to remember when it feels so awkward to place all js at the bottom.

这篇关于在底部加载JS的好处,而不是文档的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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