如果Javascript代码块不在HTML文件的末尾,而是使用jQuery的$(document).ready(function(){...}),它会减慢页面显示速度吗? [英] If Javascript code block is not at end of HTML file, but is using jQuery's $(document).ready(function() {...}), will it slow down the page display?

查看:224
本文介绍了如果Javascript代码块不在HTML文件的末尾,而是使用jQuery的$(document).ready(function(){...}),它会减慢页面显示速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说应该将Javascript代码全部放在HTML文件的末尾,以便首先显示页面内容,以便用户看到某些内容(以便用户满意地看到某些内容,而不必等待另外12个例如秒).

It is said that Javascript code should be all placed at the end of HTML file, so that the page content is shown first, for the user to see something (so that the user is satisfied to see something instead of waiting another 12 seconds, for example).

但是,为了更好地封装HTML并匹配Javascript代码(例如图片轮播"),通常将HTML和Javascript放在一个文件中,因此在整个最终脚本中都将Javascript代码块与HTML代码混合在一起HTML文件.

But to better encapsulation of the HTML and match Javascript code, such as an "Image Carousel", then usually the HTML and Javascript is placed in one single file, so there are Javascript code blocks all intermixed with HTML code throughout the final HTML file.

但是,如果所有这些Javascript代码块都使用jQuery的$(document).ready(function() { ... })来执行任务,那么页面显示也不会很快吗?我认为速度不及将Javascript实际放置在HTML文件末尾的速度快,但要足够接近,因为它只是将函数添加到了就绪事件队列中.

But what if all these Javascript code blocks use jQuery's $(document).ready(function() { ... }) to perform the task, then won't the page display be very fast as well? I think not as fast as when the Javascript is actually placed at the end of the HTML file, but close enough, because it merely adds a function to the ready event queue.

推荐答案

我认为重点是将js放在页面底部(通常只是在</body>标记内),以便页面内容在js 下载时显示.

I think the point is to place the js at the bottom of the page (usually just inside the closing </body> tag) so that the content of the page is displayed while the js is downloading.

如果您的jQuery代码在单独的.ready()调用中分布在整个HTML中,那么无论如何,直到<body>完全加载后它才能运行.所以问题是您在HTML中有多少JavaScript?

If you have your jQuery code spread out throughout the HTML in separate .ready() calls, then no matter what, it won't run until the <body> has fully loaded. So the question would be how much javascript do you have in the HTML?

如果有很多内容,那么它将减慢每个脚本之后出现的所有内容的显示.如果代码量相对较少,那么将不会有太大的变化.

If there's quite a bit, then it will slow down the display of any content that comes after each script. If it is a relatively small amount of code, then it won't likely make much noticeable difference.

如果让您尽快显示页面内容对您而言确实很重要,则将所有脚本放在之后.

If it is really important to you to have the page's content displayed as soon as possible, then place all scripts after the content.

我个人不会仅仅为了关联而将javascript与HTML混合使用.如果您开始删除/添加恰好包含脚本的内容,则可能会产生意想不到的结果.我宁愿使用适当命名的类和ID,以及大量的代码注释.

I personally wouldn't mix javascript with HTML just for the sake of association. You could have unexpected results if you start removing/appending content that happens to include a script. I'd rather use appropriately named classes and IDs, as well as lots of code comments.

还请记住,这些.ready()调用将在jQuery加载之前不起作用,这意味着它必须位于页面顶部或至少在您的第一个调用之前.

Also keep in mind that those .ready() calls won't work until jQuery has loaded, which would mean that it would need to be at the top of the page, or at least before your first call.

因此,再次回到哪个对您更重要的问题.如果您希望尽快看到内容,请将所有js放在底部.如果要混合js和HTML的方法,则在显示页面时会有一些延迟.

So again it gets back to the question of which is more important to you. If you want the content visible as quickly as possible, place all js at the bottom. If you want your method of intermixing js and HTML, you'll have some delay in displaying the page.

这篇关于如果Javascript代码块不在HTML文件的末尾,而是使用jQuery的$(document).ready(function(){...}),它会减慢页面显示速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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