ChatGPT解决这个技术问题 Extra ChatGPT

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

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>?


D
David Johnstone

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.


I have an example that moves a square around on a canvas and it will not work if I put the javascript in the head portion of the HTML. It has to be at the bottom of the body after the Canvas is declared. Is there a reason for this or how would i keep all of my Javascript in the section of the file.
@DougHauf Not sure if I'm accurate on this but have you added a DOMContentLoaded listener? I ask because if you have your script at the bottom and it renders to the canvas just fine; it's because the canvas had already been loaded to screen before you attempted to write to it. Now if you put the script in the header it wouldn't draw to the canvas because the canvas isn't there yet. So if you add the listener it'll fire your canvas code AFTER the canvas has been loaded.
I don't know how old the document you're quoting is, but it seems the defer attribute has been supported in Firefox for quite some time.
Thank you, this help me a lot
A
AHM

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.


C
Chris Love

In general you should place script references at the bottom of your page. Scripts not only need to be downloaded, they must also be evaluated and executed before the block is released and the page proceeds with the rendering process. Things like Modernizr should be placed in the top because it does some feature detections as well as HTML5 shims that you will probably want.

Another reason you want to try to place scripts at the bottom of the page is Single Points of Failure or SPOFs. This is where a script call times out or for some other reason blocks the page execution. This can happen a lot with third party advertising libraries, etc.

Yes you may have to think a little harder about how you architect your application, but I found it to become very natural very quickly for me. I have built hundreds of web apps over the past 4 years with the script at the bottom and I can tell the difference. I may be 500ms it might be 5000ms but it all matters.


a
azamsharp

It really depends on your website. If you are accessing and invoking the JavaScript functions inside the body then it must be referenced in the header so that is is loaded. Else if you are only going to call the JavaScript when the whole document is loaded then it is wise to put the JavaScript at the end of body. By putting .JS file at the end you load the whole page and then fetch the .JS file. This way the user will be able to quickly see the page and by the time he/she gets familiar with the page the .JS file has already been downloaded.


D
Draemon

Any javascript in the head will be evaluated before the page is loaded, meaning the page feels like it takes longer to load. It is slightly harder to get events to work properly if all the javascript is at the end, but jQuery pretty much solves this problem for you.


Could you mention how jQuery solves this problem out of interest?
jQuery has an API for binding handlers to events