Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's Pros and Cons: putting javascript in head and putting just before the body close

Tags:

javascript

Most of javascript and web development books/articles says that you must put CSS in the head tag and javascript at the bottom of the page.

But when I open html source of famous websites such as this one stackoverflow, I find they put some js files in the head tag.

What's Pros and Cons of both approaches and when to use which?

Found another question for the same issue: Where should I declare JavaScript files used in my page? In <head></head> or near </body>?

like image 354
Amr Elgarhy Avatar asked Mar 16 '10 00:03

Amr Elgarhy


People also ask

Is it better to put JavaScript in head or body?

The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.

Is it better to place the JavaScript code at the beginning or the end of the webpage?

The script tag should always be used before the body close or at the bottom in HTML file. The Page will load with HTML and CSS and later JavaScript will load.

Can you place JavaScript in head?

JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body. JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked.

Should scripts be inside or outside body?

Always end with a body tag and make sure every single JavaScript part is within the body tag. You can also have links or scripts inside head tags, but it is not recommended unless you are really certain your JavaScript code is too big to have it inline.


2 Answers

From Yahoo's Best Practices for Speeding Up Your Web Site:

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.

Therefore, in general, it is preferrable to put them at the bottom. However, it isn't always possible, and it often doesn't make that much of a difference anyway.

like image 143
David Johnstone Avatar answered Oct 08 '22 21:10

David Johnstone


As other people have said, when you put javascript in the head it delays the rendering of the page until after the scripts have loaded, which means the page may take longer to load - especially if you are downloading large script files.

If you move your script tags to the end of the page, you will ensure that the browser downloads images and stylesheets before the script tags and the page will likely apear to be rendered before the scripts start to run. This also means that if you are depending on some functionality from your scripts, this will not be available until a bit after the page is visible to the user.

If you are adding styles or elements (etc. switching textfields with some form of richer editor) this will be visible to the user as flickering.

If you are adding click-events to elements, they will not be clickable until a bit after the elements themselves are visible.

Sometimes theses issues requires you to put your scripts in the head, other times you will be fine by sticking them in the bottom.

IMHO (completely against YSlow and lot's of clever people) you should keep your scripts in the head tag, and just rely on them to be cached most of the time.

like image 23
AHM Avatar answered Oct 08 '22 20:10

AHM