为什么要把JavaScript放在首位 [英] Why put JavaScript in head

查看:121
本文介绍了为什么要把JavaScript放在首位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在< / body> 之前放置JavaScript是完全可以接受的,那么将它保存在< head> ;



基于< head>中的JavaScript或仅仅在< / body> ;?
之前?许多答案指出,如果将页面放在< / body> 标签。

然而,我没有看到为什么它应该保存在< head> 中的任何合理的论点。我问,因为我不是一个非常强大的JavaScript开发人员,并且从我阅读和看过的所有内容中,标准是将大多数JavaScript代码和外部引用保留在< head>

解决方案

头部中的任何东西都必须在身体加载前完成,所以它通常是一个糟糕的主意把JavaScript放在那里。如果您在正在加载时需要,或者想要加快某个Ajax,那么将它放在头部是适当的。


If it is perfectly acceptable to put JavaScript right before </body> what is a good reason to keep it in the <head>?

Based on the question JavaScript in <head> or just before </body>? many answers state that the page will load faster if you put it right before the </body> tag.

However I don't see any sound arguments on why it should be kept in the <head>. I'm asking because I am not a very strong JavaScript developer and from everything I've read and seen, the standard is to keep most JavaScript code and external references in the the <head>.

解决方案

Anything in the head must be completed before the body is loaded, so it is generally a bad idea to put javascript in there. If you need something while the body is loading, or want to expedite some ajax, then it would be appropriate to put it in the head.

这篇关于为什么要把JavaScript放在首位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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