我应该在哪里声明我的页面中使用的JavaScript文件?在< head>< / head>或接近< / body&gt ?? [英] Where should I declare JavaScript files used in my page? In <head></head> or near </body>?

查看:124
本文介绍了我应该在哪里声明我的页面中使用的JavaScript文件?在< head>< / head>或接近< / body&gt ??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读教程,作者提到要在接近 body 标签(< / body> 在HTML中)。



我想知道什么类型的功能不应该在头部部分声明/定义JavaScript。在结束 body 标记附近添加JavaScript Analytics之类的JavaScript是很有意义的。我应该在哪里定义JavaScript,包括在 body 标签附近?

解决方案

通常会认为,出于速度的目的,你应该把脚本标签放在文档的最后(在关闭的主体标签之前)。虽然这会导致页面加载速度最快,但它有一些严重的缺点。

首先,网页开发的常见习惯是拥有一个头文件,一个页脚文件和你的内容在中间。为了将不必要的Javascript保持在最低限度,您通常希望将代码片段放在单独的页面中。



例如,如果在文档的末尾包含jquery ,你的jquery代码片段(如文档准备好的东西)必须在之后发生。第二,根据我的经验,由于页面加载速度更快,您最终可能会注意到正在应用某些效果,因为例如,如果你在文档中放置一个表格,并在正文关闭标记之前放置:

  $(function(){
$(tr:nth-​​child(odd))。addClass(odd);
});

与适当的样式,应用的效果往往是可见的。我个人认为这可能会导致不良的用户体验。我认为如果你没有感到令人不安的视觉效果,通常情况下你最好把页面加载速度放缓(通过将脚本放在最上面)。



我一般都会提倡有效的缓存策略,因此只需在更改时下载Javascript文件,如在PHP中增加JavaScript脚本(但是这些原则适用于任何语言,不仅仅是PHP)并且仍然将脚本放在顶部。这非常方便。

I was reading a tutorial and the author mentioned to include Javascript files near closing body tag (</body>) in HTML.

I was wondering for what type of functionality I should not declare/define JavaScript include in the head section? It makes sense to me include JavaScript like Google Analytics near the closing body tag. Where should I be careful in defining JavaScript include near the closing body tag?

解决方案

It will often be argued that for speed purposes you should put script tags right at the end of the document (before the close body tag). While this will result in the fastest page load, it has some serious downsides.

Firstly, a common idiom with Webpage development is to have a header file, a footer file and your content in the middle. To keep unnecessary Javascript to a minimum, you'll often want to put code snippets in individual pages.

If you include jquery, for example, at the end of the document, your jquery code snippets (like document ready stuff) must happen after that. That can be awkward from a development point of view.

Secondly, in my experience, because the page load is faster, you can end up noticing certain effects being applied because the page has already loaded by the time they are applied.

For example if you put a table in a document and right before the body close tag put:

$(function() {
  $("tr:nth-child(odd)").addClass("odd");
});

with appropriate styling, that effect being applied will often be visible. Personally I think that makes for a bad user experience potentially. I think often you're better off having the page load slightly slower (by putting scripts at the top) if you don't get disconcerting visual effects.

I generally advocate effective caching strategies so you only have to download Javascript files when they change, as in Supercharging Javascript in PHP (but the principles apply to any language, not just PHP) and still putting scripts at the top. It's far more convenient.

这篇关于我应该在哪里声明我的页面中使用的JavaScript文件?在&lt; head&gt;&lt; / head&gt;或接近&lt; / body&gt ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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