开发者

should jQuery files always be inside <head>?

开发者 https://www.devze.com 2023-02-10 09:07 出处:网络
Should the jquery related code external file (plugins etc.) always be in head of the开发者_如何转开发 html or it can be included somewhere in body as well?Not necessarily. Traditionally, the javascrip

Should the jquery related code external file (plugins etc.) always be in head of the开发者_如何转开发 html or it can be included somewhere in body as well?


Not necessarily. Traditionally, the javascript code is to be put in <head> tags but you can put it in the <body> tag just as well.

As per Yahoo performance article, you should put your code at the end of the page just before </body> tag.

Quoting:

Put Scripts at the Bottom

The problem caused by scripts is that they block parallel downloads. The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel. While a script is downloading, however, the browser won't start any other downloads, even on different hostnames.

In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page. There might also be scoping issues. In many cases, there are ways to workaround these situations.

An alternative suggestion that often comes up is to use deferred scripts. The DEFER attribute indicates that the script does not contain document.write, and is a clue to browsers that they can continue rendering. Unfortunately, Firefox doesn't support the DEFER attribute. In Internet Explorer, the script may be deferred, but not as much as desired. If a script can be deferred, it can also be moved to the bottom of the page. That will make your web pages load faster.


If you are manipulating the DOM, you need to make sure javascript code is able to find the DOM elements by placing it at the appropriate place. You may also use window.onload event for that.


You can use a SCRIPT element either in HEAD, BODY, or wherever an inline element is allowed.

Additionally, many suggest to put SCRIPT at the end of BODY to improve loading of the document. You can also use tools like Head JS to improve the loading even more.


It does not need to be in the and can be at the bottom of the page. For performance reasons it's better to load the jquery at the end.


jquery is javascript and so it should work in the body, however, a good coding standard for oop programmers like myself, is to put it in a different file if at all possible and only use the head for small things. I don't tend to use the body if i can help it as there is usually so much going on in there! What is it you are trying to do?


No they don't, but remember that anything inside the <head> section loads before elements in the <body> do.


Can be included but in <script type="text/javascript" language="javascript"></script> tags

Look at this article:

  • Best practices for modern Javascript development
    • Use the correct tag
    • Keep your code in an external file
    • Don’t wrap code in HTML comments
    • Always declare your variables using var
    • Keep your code unobtrusive
    • Include your scripts at the bottom of your HTML files
    • Use JSLint
    • Don’t use document.write
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号